Javascript 吴希德;AngularJS的ng秀
使用AngularJS,我想以切换方式显示和隐藏与特定id相关的数据 我的JSON数据格式如下:Javascript 吴希德;AngularJS的ng秀,javascript,html,angularjs,ng-hide,angularjs-ng-show,Javascript,Html,Angularjs,Ng Hide,Angularjs Ng Show,使用AngularJS,我想以切换方式显示和隐藏与特定id相关的数据 我的JSON数据格式如下: $scope.things = [{ id: 1, data: 'One', shown: true }, { id: 2, data: 'Two', shown: false }, { id: 3, data: 'Three', sho
$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并隐藏其他文本,依此类推
这是我试过的小提琴:我更新了你的代码
$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=函数(id){
$scope.things.forEach(函数(thing){
if(id==thing.id){
所显示的事物=真实;
}
否则{
显示的事物=虚假;
}
})
};
这是工作程序它应该工作
$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>
$scope.flipMode=函数(id){
$scope.things.forEach(函数(thing){
if(thing.id==id){
所显示的事物=真实;
返回;
}
显示的事物=虚假;
})
};
分叉工作解决方案:
更改范围功能:
$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>
谢谢。是否可以在该id下方显示相关数据?{{thing.data}