[英]set array element object property based on the presence of an element of another array with the same id using array methods
[英]Angularjs - showing element based on a presence of id in array
anyoen是否知道如何檢查(在視圖中)給定數組中是否存在id?
可以說我有一個像這樣的數組:
var arr = [123, 456, 789];
和這樣的數據源:
var data = [
{id: 123, name: 'foo'},
{id: 456, name: 'bar'},
{id: 789, name: 'baz'}
];
現在在迭代data
數組的視圖中,我想基於arr
中id
的存在來顯示/隱藏元素,如下所示:
<div ng-repeat="item in data">
<span ng-show="item.id in arr"></span>
</div>
上面的代碼item.i in arr
當然不能用於顯而易見的原因。 任何人都知道如何實現該功能? 更不用說ng-show塊應該始終在arr
數組被改變時啟動。
提前致謝。
您可以在范圍內創建方法
function MyCtrl( $scope ) {
$scope.data = ...
$scope.enabled = ...
$scope.visible = function (id ) {
return $scope.enabled.indexOf(id) > -1
}
}
和
<span ng-show="visible(item.id)" ></span>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.