繁体   English   中英

如何使用Angular获得价值并在select中选择文本

[英]How to get value and select text in select with Angular

我有以下选择(翡翠):

select(ng-model="note.shop1", ng-change="selectShop()", ng-disabled="allShops" ng-show="positionAvailable")
   option(ng-repeat="shop in nearbyShops" value="{{shop._id}}")
     {{shop.name}} ({{shop.contact_address}} {{shop.contact_housenr}}, {{shop.contact_city}})
   option(value="otherShop") My store isn't listed.

我的控制器中的Selectshop():

$scope.selectShop = function(){

        if($scope.note.shop1 == "otherShop"){
            $scope.allShops = true;
        }
}

我想存储选定的ID,所以可以保存它,但是我想在用户选择商店时向用户显示{{shop.name}} 我怎样才能做到这一点?

因此,我要选择{{shop._id}} ,然后选择{{shop.name}}保存。

您只需从值中删除._id即可在ng-change方法中传递对象。 尝试这个。

select(ng-model="note.shop1", ng-change="selectShop(note.shop1)", ng-disabled="allShops" ng-show="positionAvailable")
   option(ng-repeat="shop in nearbyShops" value="{{shop}}")
     {{shop.name}} ({{shop.contact_address}} {{shop.contact_housenr}}, {{shop.contact_city}})

在您的selectShop方法中。 得到他们

$scope.selectShop = function(selectedShop){
    console.log(selectedShop._id, selectedShop.name);
        if($scope.note.shop1 == "otherShop"){
            $scope.allShops = true;
        }
}

我做了一个演示小提琴来演示。

希望能帮助到你。

我可以建议和HTML版本来解决您的问题。

 <select ng-model="selectedShop._id" ng-options="selectedShop._ID as selectedShop.name for selectedShop in nearbyShops" 
   ng-change=selectShop(selectedShop,{{selectedShop._id}})></select>

你的功能将是

$scope.selectShop = function(shop){
// your selected shop id will be $scope.shop._id

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM