2014-09-05 56 views
0

當用戶在下拉菜單中選擇一個值時,我需要讓我的控制器執行一些操作。如何獲得ng選項來選擇運行功能?

編輯:1.3提供getterSetterngModelOption,但我需要使用1.2.x.編輯刪除錯誤的1.3兼容的代碼。

HTML:

<select ng-model="selectedThing" 

     ng-options="thing.name for thing in allThings"> 
</select> 
+0

不該」它是'getterSetter'的大寫字母嗎? – PSL 2014-09-05 23:41:45

+0

適用於我.. http://plnkr.co/edit/o2AuIl?p=preview你有其他問題檢查你的控制檯是否有任何錯誤。那麼你使用的角度版本是否支持它?嘗試在重擊中複製您的問題 – PSL 2014-09-05 23:47:53

+0

控制檯中沒有錯誤,這很奇怪。我現在和你的朋友一起玩。 – ray 2014-09-05 23:52:17

回答

0

對於1.2.x的

只有做到這樣,似乎是$watch荷蘭國際集團的$scope.selectedThing

$scope.selectedThing = aThing; 

$scope.$watch('selectedThing', function() { 
    //do stuff here 
}); 

一旦你在下拉列表中選擇一個值,這將觸發。

1.3-β

如果你想避免$watch荷蘭國際集團的變量,那麼你可以使用即將ngModelOptions

<select ng-model="selectedThing" 
     ng-model-options="{getterSetter:true}" 
     ng-options="thing.name for thing in allThings"> 

而在你的控制器:

currentThing = aThing; 

$scope.selectedThing = function (newThing) { 
    if (angular.isDefined(newThing)) { 
     console.log("setting new Thing", newThing); 
     currentThing = newThing; 

    } 
    return currentThing; 
};