角NG-重复跳过某个项目是否匹配前pression [英] angular ng-repeat skip an item if it matches expression

查看:141
本文介绍了角NG-重复跳过某个项目是否匹配前pression的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方式来大致看出角在NG-重复跳过一个项目,如果它匹配一个前pression,基本上继续;

I'm looking for a way to basically tell angular to skip an item in an ng-repeat if it matches an expression, basically continue;

在控制器:

$scope.players = [{
    name_key:'FirstPerson', first_name:'First', last_name:'Person'
}, {
    name_key:'SecondPerson', first_name:'Second', last_name:'Person'
}]

现在在我的模板我想告诉大家不匹配 name_key ='第一人称'。我想通它必须是过滤器,所以我设置一个Plunkr玩弄它,但没有任何运气。 Plunkr尝试

Now in my template I want to show everyone that doesn't match name_key='FirstPerson'. I figured it has to be filters so I setup a Plunkr to play around with it but haven't had any luck. Plunkr Attempt

推荐答案

@Maxim Shoustin 建议的,最好的方式来达到你想要是使用自定义过滤器的东西。结果
但也有其他的方式,其中之一是使用 NG-如果在相同的元素指令是你把 NG-重复指令(还有,这里的 plunker ):

As @Maxim Shoustin suggested, the best way to achieve what you want would be to use a custom filter.
But there are other ways, one of them being to use the ng-if directive on the same element were you put the ng-repeat directive (also, here's the plunker):

<ul>
    <li ng-repeat="player in players" ng-if="person.name_key!='FirstPerson'"></li>
</ul>

这可能present从estetical角度来看,小的不足,但你的过滤可以基于不紧耦合到玩家数组,可以轻松地在应用程序的范围内访问其他数据:

This may present a minor disadvantage from an estetical perspective, but has a major advantage that your filtering could be based on a rule that is not as tight coupled to the players array and that can easily access other data in your app's scope:

  <li 
      ng-repeat="player in players" 
      ng-if="app.loggedIn && player.name != user.name"
  ></li>

更新结果
如前所述,这是的解决方案作为此类问题之一,可能会或可能不适合你的需求。结果
正如在评论中指出, NG-如果是一个指令,它实际上意味着它可能在你可能期望的背景做更多的事情。结果
例如, NG-如果 创建一个新的范围,从它的父

Update
As stated, this is one of the solutions for this kind of problem and may or may not suit your needs.
As pointed out in the comments, ng-if is a directive, which actually means that it might do more things in the background as you might expect.
For example, ng-if creates a new scope from it's parent:

中ngIf创建范围使用原型继承其父继承范围

The scope created within ngIf inherits from its parent scope using prototypal inheritance.

这通常不影响正常的行为,但以prevent意想不到的情况下,应在实施之前记住这一点。

This usually doesn't affect the normal behaviour but in order to prevent unexpected cases, you should keep this in mind before implementing.

这篇关于角NG-重复跳过某个项目是否匹配前pression的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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