Angularjs - 表示基于在阵列ID的presence元件 [英] Angularjs - showing element based on a presence of id in array

查看:130
本文介绍了Angularjs - 表示基于在阵列ID的presence元件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请问anyoen知道如何检查(在视图)的ID的presence一个给定的数组中?

Does anyoen know how to check (in the View) for a presence of id in a given array?

可以说我有一个数组,像这样:

Lets say I have an array like so:

var arr = [123, 456, 789];

和数据源是这样的:

var data = [
  {id: 123, name: 'foo'},
  {id: 456, name: 'bar'},
  {id: 789, name: 'baz'}
];

在视图

现在同时遍历数据阵列我想根据 ID的presence显示/隐藏的元素改编像这样:

Now in the view while iterating over the data array I would like to show/hide elements based on a presence of id in the arr like so:

<div ng-repeat="item in data">
    <span ng-show="item.id in arr"></span>
</div>

以上code item.i在改编当然,这并不显而易见的原因工作。任何人都知道如何实现这个功能?更何况吴秀块要经常踢在每当改编阵列被改变。

The above code item.i in arr of course does not work for obvious reasons. Anyone know how to achieve that functionality? Not to mention the ng-show block should always kick-in whenever the arr array is altered.

先谢谢了。

推荐答案

您只需在你的范围内创建一个方法

You can just create a method in your scope

function MyCtrl( $scope ) {
  $scope.data = ...

  $scope.enabled = ...


  $scope.visible = function (id ) { 
    return $scope.enabled.indexOf(id) > -1
  } 

}

<span ng-show="visible(item.id)" ></span>

这篇关于Angularjs - 表示基于在阵列ID的presence元件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆