如何通过点击更改ng-model?

时间:2015-09-12 10:38:14

标签: angularjs

我有ng-click事件:

ng-click="changeSpecializationStatus(specializationMain[$index], $index)"

Angular JS:

$scope.changeSpecializationStatus = function (item, index){
    $scope.specializationMain[index] = (item == true) ? false : true;
}

我希望在每次点击后在truefalse上使用$ index更改元素中的值

1 个答案:

答案 0 :(得分:0)

您也可以这样做 我已经使用索引和布尔值传递数组,并将其显示在ng-repeat中 HTML:

<html>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="myCtrl">
<input type = "button" value = "{{s.index}}" ng-repeat = "s in specializationMain " ng-click = "changeSpecializationStatus(s.index)">
</div>

</body>
</html>

JS:

    var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {

    $scope.specializationMain = [{"index" : 1, "value": "false"},{"index" : 2, "value": "true"}];


  $scope.changeSpecializationStatus = function (index){
    //alert(index);
    $scope.specializationMain[index - 1].value =!$scope.specializationMain[index - 1].value;
    alert($scope.specializationMain[index - 1].value);
  }
});

您可以登入codepen: http://codepen.io/anon/pen/OyydRo