[英]ng-hide & ng-show in AngularJS
我想使用AngularJS以切换方式显示和隐藏与特定ID相关的数据。
我的JSON数据格式如下:
$scope.things = [{
id: 1,
data: 'One',
shown: true
}, {
id: 2,
data: 'Two',
shown: false
}, {
id: 3,
data: 'Three',
shown: true
}, ];
我想要的是单击id-1时将显示文本1和隐藏其他,单击id-2时将显示文本2和隐藏其他,依此类推。
这是我尝试的小提琴: jsfiddle:演示链接
我更新了您的代码
$scope.flipMode = function (id) {
$scope.things.forEach(function (thing) {
if(id == thing.id){
thing.shown = true;
}
else{
thing.shown = false;
}
})
};
<a href="#" ng-click="flipMode(thing.id)">{{thing.id}}</a>
这是工作的小提琴
它应该工作
$scope.flipMode = function (id) {
$scope.things.forEach(function (thing) {
if(thing.id === id) {
thing.shown = true;
return;
}
thing.shown = false;
})
};
<div ng-repeat="thing in things">
<a href="#" ng-click="flipMode(thing.id)">{{thing.id}}</a>
</div>
分叉的工作解决方案: http : //jsfiddle.net/nypmmkrh/
更改您的范围功能:
$scope.flipMode = function (id) {
$scope.things.forEach(function(thing) {
if(thing.id == id) {
thing.shown = true;
} else {
thing.shown = false;
}
});
};
并在视图中传递ID:
<a href="#" ng-click="flipMode(thing.id)">{{thing.id}}</a>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.