如何获取值并使用Angular选择文本

时间:2016-08-19 06:56:09

标签: javascript angularjs

我有以下选择(在玉中):

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}}已保存。

2 个答案:

答案 0 :(得分:0)

您只需从值中删除._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;
        }
}

我做了demo fiddle来演示。

希望它有所帮助。

答案 1 :(得分:0)

我可以为您的问题建议和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

}