2016-08-19 5 views
0
角度

を選択してテキストを選択する方法:値を取得し、私は(ヒスイ)で次を選択している

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. 

セレクト()私のコントローラで:私は保存するために

$scope.selectShop = function(){ 

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

たい選択したIDを保存することができますが、店を選択したときにユーザーに{{shop.name}}を表示します。 どうすればいいですか?

したがって、{{shop._id}}を選択し、{{shop.name}}を保存するように選択します。

+0

あなたは私にあなたの 'セレクト()'メソッドを表示することができますでしょうか?店舗IDと店舗名の両方を正しく保存したいのですか? –

+0

私のポストにいくつかのコードを追加しました。私は何かを忘れて、はい、私は両方を保存したい。 – NVO

答えて

0

ng-changeメソッドでオブジェクトを渡す値から._idを削除するだけです。これを試して。

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を作りました。

希望します。

+0

selectShopでconsole.log(shop)を実行すると、ショップ全体が表示されますが、console.log(shop.name)を実行すると「未定義」になります。何がうまくいかないのですか? :) – NVO

+0

'selectShop()'メソッドで 'console.log(selectedShop._id、selectedShop.name);'を試してください –

+0

うん、2 '未定義の' – NVO

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 

} 
関連する問題