修改除一个以外的所有$ scope.thing

时间:2014-11-19 23:48:21

标签: javascript html angularjs dom angularjs-scope

我有一个ul,其li填充ng-repeat = "thing in things"。每个li都有一个由true定义的false$scope.isTrue = true;值。当用户点击li时,我想将其$scope.isTrue值更改为false。如果用户点击其他li我想将 $scope.isTrue值更改为false,并将可能false的任何其他值更改为{ {1}}。

到目前为止,这是我的代码(http://jsfiddle.net/HB7LU/8401/):

HTML:

true

角:

<ul>                                         
  <li ng-repeat="thing in things" >                 
    <p ng-click="changeVal()">{{isTrue}}</p>
  </li>
</ul>

http://jsfiddle.net/HB7LU/8401/

目标是一次只有一个$scope.isTrue = true; $scope.changeVal = function() { if (this.isTrue == true) { $scope.isTrue = true; //I try to change them all to true this.isTrue = false; //but nothing happens. } else if (this.isTrue == false) { $scope.isTrue = true; this.isTrue = true; } }; 值。谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

为什么不做那样的事情:

<div ng-controller="MyCtrl">
<ul>                                         
  <li ng-repeat="(key,val) in things" >                 
    <p ng-click="changeVal(key)">{{val}}</p>
  </li>
</ul>
</div>

var myApp = angular.module('myApp',[]);
function MyCtrl($scope) {    
    $scope.things = { 
        0: true, 
        1: true, 
        2: true
    }

  $scope.changeVal = function(key) {
    $scope.things[key] = false
    for (k in $scope.things){
        if (k!= key && $scope.things[k] == false){
          $scope.things[k] = true
        }
    }
  };
}

请参阅fiddle

相关问题