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

时间:2022-07-05 20:32:27

I have the following select (in jade):

我有以下选择(在玉):

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() in my controller:

我的控制器中的Selectshop():

$scope.selectShop = function(){

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

I want to store the selected ID, so I can save it, but I want to show the {{shop.name}} to the user when he selected a shop. How can I do that?

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

So, I want selected {{shop._id}}, and select {{shop.name}} saved.

所以,我希望选择{{shop._id}},然后选择{{shop.name}}保存。

2 个解决方案

#1


0  

You just have to remove the ._id from the value to pass the object in your ng-change method. Try this.

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

and In your selectShop method. get them by

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

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

I've made a demo fiddle to demonstrate.

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

Hope it helps.

希望能帮助到你。

#2


0  

I can suggest and HTML version for your problem.

我可以为您的问题建议和HTML版本。

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

and your function will be

你的功能将是

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

}

#1


0  

You just have to remove the ._id from the value to pass the object in your ng-change method. Try this.

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

and In your selectShop method. get them by

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

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

I've made a demo fiddle to demonstrate.

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

Hope it helps.

希望能帮助到你。

#2


0  

I can suggest and HTML version for your problem.

我可以为您的问题建议和HTML版本。

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

and your function will be

你的功能将是

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

}