【发布时间】:2018-03-21 12:33:29
【问题描述】:
我在 AngularJS ng-repeat 中使用 json 数组。我观察到它订购的输入覆盖了我在我的 html 中的内容。有什么办法可以防止这种排序发生吗?
这是我的代码(在Plunkr 中查看):
<body ng-controller="Controller">
<div ng-repeat="x in data">
<input type="checkbox" ng-show="x.Project == 'ABC'" ng-model="x.Show"><b ng-show="x.Project == 'ABC'"> project ABC is wonderful</b>
<input type="checkbox" ng-show="x.Project == 'DEF'" ng-model="x.Show"><b ng-show="x.Project == 'DEF'"> How is this EFG?</b>
<input type="checkbox" ng-show="x.Project == 'IJK'" ng-model="x.Show"><b ng-show="x.Project == 'IJK'"> This is my IJK project</b>
</div>
<br> {{data}}
</body>
还有数据:
$scope.data = [{
"Project": "IJK",
"Show": "true"
},
{
"Project": "DEF",
"Show": "false"
},
{
"Project": "ABC",
"Show": "true"
}
];
我找到了solution,但这是使用外部函数,但如果可能的话,我正在寻找更好的方法。
请提出建议。
【问题讨论】:
-
你为什么不在
data上创建一个ng-repeat并使用其内容动态生成intputs,而不是在每次迭代中显示/隐藏它们中的三个(由方式,你知道你正在生成 9 个输入,并且只显示 3 个吗?)。这将以 json 的相同顺序显示值。
标签: angularjs angularjs-ng-repeat