如何检测JSON模型中的属性值是否被AngularJS中的给定值所改变

How to detect if the value of a property in an JSON Model is changed with a given value in AngularJS

本文关键字:AngularJS 是否 改变 何检测 属性 检测 JSON 模型      更新时间:2023-09-26

我有一个给定的json模型名为"实体"例如:

var entities =
[{"name":"Test1","isDirty":false},
{"name":"Test2","isDirty":false},
{"name":"Test3","isDirty":true}]

我需要通过使用ng-show来显示下面的列表,当使用模型实体检测到isDirty为TRUE的对象时,下面的代码不起作用。我只使用这个元素一次,没有重复使用。

<li ng-show="entities[isDirty:true]">Click <span class="red">'View Errors'</span> to finalize results.</li>

我使用的是最新的AngularJS库

您应该添加一个检查isDirty的函数

$scope.checkDirties = function() {
var length = $scope.entities.length;
for (var i=0; i<length; i++) {
if ($scope.entities[i].isDirty) return true;
}
return false;
};
使用

ng-show = " checkDirties ()"

您也可以这样做:

<ul>
<li ng-repeat="entry in entries" ng-show="entry[$index].isDirty"></li>
</ul>

当你这样做时,当条目对象发生变化时它会相应地在UI上呈现。一定要在$scope上定义条目对象。