2015-02-05 20 views
0

我想用按鈕更改選定選項。如果我沒有在參數中使用數據綁定,代碼將按預期工作:Angular.js - 將具有數據綁定功能的按鈕中的選定選項更改爲函數參數

ng-click="changeOptions('cat');" 

如果我對參數使用數據綁定,它將不再起作用。這是不允許的?

ng-click="changeOptions('{{animals.type}}');" 

下面是HTML:

<div ng-app="quickApp" ng-controller="quickController"> 
    <select ng-model="animalList" ng-options="animals.type as animals.type for animals in animals"></select> 
    <button ng-repeat="animals in animals" ng-click="changeOptions('{{animals.type}}');">{{animals.type}}</button> 
</div> 

這裏是JS:

angular 
    .module('quickApp', []) 

    .controller('quickController', ['$scope', function($scope) { 

     $scope.changeOptions = function(id){ 
       $scope.animalList = id; 
      }; 

     $scope.animals = [ 
       {'type':'cat'}, 
       {'type':'dog'}, 
       {'type':'bear'} 
     ]; 
    }]); 

回答

0

你真的不需要這些大括號內。

ng-click="changeOptions(animals.type);" 

應工作

順便說一句,你爲什麼不改變animals in animalsanimal in animals?兩個animals似乎不明確。在這種情況下,您必須使用:

<button ng-repeat="animal in animals" ng-click="changeOptions('{{animal.type}}');">{{animal.type}}</button> 
+0

謝謝,您的建議幫助我達到了現在正確運行的程度。 最初令人困惑的是,在查看DOM時,都使用「字符串」和「{數據綁定}」作爲參數呈現完全相同,但獲得了不同的結果。 – sjmartin

相關問題