Javascript 修改除一个以外的所有$scope.thing
我有一个Javascript 修改除一个以外的所有$scope.thing,javascript,html,angularjs,dom,angularjs-scope,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的值更改为true 以下是我到目前为止的代码(): html: 目标是一次有一个false值。谢谢你的帮助。为什么不这样做: <div
ul
,其中填充了li
,由ng repeat=“thing in things”
组成。每个li
都有一个true
或false
值,该值由$scope.isTrue=true定义代码>。当用户单击li
时,我想将其$scope.isTrue
值更改为false
。如果用户单击不同的li
我想将其$scope.isTrue
值更改为false
,并将任何其他可能为false
的值更改为true
以下是我到目前为止的代码():
html:
目标是一次有一个false
值。谢谢你的帮助。为什么不这样做:
<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
}
}
};
}
-
{{val}
var myApp=angular.module('myApp',[]);
函数MyCtrl($scope){
$scope.things={
0:对,
1:对,
2:对
}
$scope.changeVal=函数(键){
$scope.things[key]=false
for(k在$scope.things中){
if(k!=key&&$scope.things[k]==false){
$scope.things[k]=true
}
}
};
}
另请参见“每个li都有一个由$scope.isTrue=true定义的真值或假值;
”modolethings
是一个数组,但$scope.isTrue
不是这个。isTrue
在这里做什么<代码>此
在控制器中引用控制器的实例
$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;
}
};
<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
}
}
};
}