所以这就是我的问题,我使用AngularJS,我从PHP获取JSON,并使用ng-repeat显示我的所有数据。我已经这样做了。
现在我想检查一些数据是否在" Array1"如果是,则更改ng-repeat中的相应数据。我知道这听起来很奇怪,但让我举个代码示例:
这里是array1值
{
id : "23",
name: "example"
}
所以当我得到数据时,它是这样的:
<div ng-model="data.posts" ng-repeat="post in posts | orderBy:'-' | unique: 'id'">
...
<button>This button will show if "id" matches</button>
<button>This button will show if "id" not matches</button>
</div>
对于每个JSON对象,我都使用ng-repeat来显示它们:
{{1}}
我想比较一下array1的id是否与JSON数据中的id匹配,如果匹配则显示一个按钮,如果不显示其他按钮。
我这就像2个星期一样,我无法解决问题,而且我也没有办法解决问题。
至少读书感谢抱歉我的英语不好!
答案 0 :(得分:1)
您的array1应该是一个数组,并且可以在控制器中添加一个函数来检查匹配ID。
控制器中的:
$scope.array1 = ["23","48","51"];
$scope.checkInArray1 = function(id) {
var index = $scope.array1.indexOf(id);
if(index < 0){
return false;
} else {
return true;
}
};
并在你的HTML中:
<button ng-if="checkInArray1(post.id)">This button will show if "id" matches</button><br>
<button ng-if="!checkInArray1(post.id)">This button will show if "id" not matches</button>
答案 1 :(得分:0)
假设{“23”,“48”,“51”}应为数组[“23”,“48”,“51”] 你可以这样做: 工作小提琴:http://jsfiddle.net/ravenous52/rgyom4yd/
myApp.controller('MyCtrl', ['$scope',
function($scope) {
$scope.knownIds = ["23", "48", "51"];
$scope.data = {
posts: [{
id: "23",
name: "example23"
}, {
id: "51",
name: "example51"
}, {
id: "99",
name: "example99"
}]
}
}
]);
<section ng-controller="MyCtrl">
<div ng-repeat="post in data.posts">
<button ng-show="knownIds.indexOf(post.id) >-1">This button will show if "id" matches</button>
<button ng-hide="knownIds.indexOf(post.id) >-1">This button will show if "id" not matches</button>
</div>
</section>
答案 2 :(得分:0)
https://jsfiddle.net/alair016/4wc44on1/
<div ng-app='myApp' ng-controller="MyCtrl">
<div ng-model="data.posts" ng-repeat="post in data.posts">
<button ng-if="array1.indexOf(post.id) >-1">{{post.name}} id matches</button>
<button ng-if="array1.indexOf(post.id) == -1">{{post.name}} id does not match</button>
</div>
</div>
var myApp = angular.module('myApp',[])
.controller('MyCtrl', ['$scope', function($scope) {
$scope.array1 = ["23","48","51"];
$scope.data = {
posts : [
{
id : "23",
name: "example"
},
{
id: "24",
name:"second example"
}
]
};
}]);