将ng-model绑定到按钮值

时间:2017-08-30 22:50:13

标签: angularjs angular-ngmodel

我试图绑定这两个按钮的值 - "是"和"否" - 使用formData点击ng-model但没有运气。除了使用ng-model之外,有没有办法实现这个目标?

<button class="" 
  ng-class="{'button--is-selected--turquoise': isSelected, 
             '' : isSelected===false}" 
  ng-click="isSelected = true" 
  ng-model="formData.yesPool">Yes
</button>

<button class="" 
  ng-class="{'button--is-selected--turquoise': 
  isSelected === false, 'notSelected' : isSelected }" 
  ng-click="isSelected = false" 
  ng-model="formData.noPool"">No
</button>

这是我的控制人员:

angular
  .module('testApp')
  .controller('formController', ['$scope', '$http', function($scope, $http) {
    $scope.formData = {};
    $scope.processForm = function(){
    };
  }]);

1 个答案:

答案 0 :(得分:5)

<script type="text/javascript"> var list = <%-JSON.stringify(events)%>; </script> 仅适用于接受用户输入的元素,不包括按钮。如果要在单击按钮时设置值,可以将其放在ng-model

ng-click

或者将它们放在函数中

<button class="" 
  ng-class="{'button--is-selected--turquoise': isSelected, 
             '' : isSelected===false}" 
  ng-click="isSelected = true; formData.yesPool = true">Yes
</button>

<button class="" 
  ng-class="{'button--is-selected--turquoise': 
  isSelected === false, 'notSelected' : isSelected }" 
  ng-click="isSelected = false; formData.noPool = true">No
</button>

希望有所帮助