简体   繁体   English

如何将 ng-if 与 ng-repeat 一起使用?

[英]How to use ng-if with ng-repeat?

I have a simple nav object setup that lists the nav items (and whether they should appear in the primary nav or not).我有一个简单的导航对象设置,它列出了导航项目(以及它们是否应该出现在主导航中)。 It seems though when I try to mix ng-if with ng-repeat, things fall apart, but when I mix ng-show with ng-repeat it works fine (but I end up with a bunch of hidden elements that I don't want appended to the DOM).似乎当我尝试将 ng-if 与 ng-repeat 混合时,事情会分崩离析,但是当我将 ng-show 与 ng-repeat 混合时,它工作正常(但我最终得到了一堆我不知道的隐藏元素想要附加到 DOM)。

   <section class="nav">
        <a  ng-repeat="(key, item) in route.routes"
            ng-href="{{key}}"
            ng-show="item.nav"
        >
                {{item.label}}
        </a>
    </section>

But the following doesn't work (note the ng-show is now ng-if ):但以下不起作用(注意ng-show现在是ng-if ):

    <section class="nav">
    <a  ng-repeat="(key, item) in route.routes"
        ng-href="{{key}}"
        ng-if="item.nav"
    >
            {{item.label}}
    </a>
</section>

The routes object looks like路线对象看起来像

routes: {
    '/home': { label: 'Home', nav: true },
    '/contact': { label: 'Contact', nav: false},
   // etc
}

I receive the following error when trying to use ng-if :尝试使用ng-if时收到以下错误:

Error: Multiple directives [ngIf, ngRepeat] asking for transclusion on:错误:多个指令 [ngIf, ngRepeat] 要求对以下内容进行嵌入:

I guess it's trying to tell me that I can't state it's declaration for existing twice.我想它是想告诉我,我不能说它是两次存在的声明。 I could use ng-if on an inner element, but I think I would still end up with a bunch of empty outer a tags.我可以在内部元素上使用ng-if ,但我想我最终还是会得到一堆空的外部a标签。

There's probably a better solution, but after reading the replies above, you can try making your own custom filter:可能有更好的解决方案,但是在阅读上面的回复后,您可以尝试制作自己的自定义过滤器:

angular.module('yourModule').filter('filterNavItems', function() {
  return function(input) {
    var inputArray = [];

    for(var item in input) {
      inputArray.push(input[item]);
    }

    return inputArray.filter(function(v) { return v.nav; });
  };
});

Then to use it:然后使用它:

<section class="nav">
    <a  ng-repeat="(key, item) in routes | filterNavItems"
        ng-href="{{key}}">
            {{item.label}}
    </a>
</section>

Here's the Plunker: http://plnkr.co/edit/srMbxK?p=preview这是Plunker: http ://plnkr.co/edit/srMbxK?p=preview

而不是ng-if你应该在你的ng-repeat上使用过滤器( http://docs.angularjs.org/api/ng.filter:filter )来从你的列表中排除某些项目。

I ran into this problem as well, and found a couple ways to solve it.我也遇到了这个问题,并找到了几种解决方法。

The first thing I tried was to combine ng-if and ng-repeat into a custom directive.我尝试的第一件事是将ng-ifng-repeat成一个自定义指令。 I'll push that up to github sometime soon, but it's kludgy.我很快就会把它推送到 github,但它很笨拙。

The simpler way to do it is to modify your route.routes collection (or create a placeholder collection)更简单的方法是修改您的route.routes集合(或创建一个占位符集合)

$scope.filteredRoutes = {};
angular.forEach($scope.route.routes, function(item, key) {
    if (item.nav) { $scope.filteredRoutes[key] = item; }
};

and in your view在你看来

...
<a  ng-repeat="(key, item) in filteredRoutes"
...

If you need it to be dynamically updated, just set up watches, etc.如果您需要动态更新,只需设置手表等。

How about this one-liner using $filter :这个单行如何使用$filter

$scope.filteredRoutes = $filter('filter')($scope.route.routes, function(route){
  return route.nav;
});

You should use a filter in your ng-repeat instead of using ng-if .您应该在ng-repeat中使用过滤器,而不是使用ng-if

This should work:这应该有效:

<section class="nav">
    <a  ng-repeat="(key, item) in route.routes | filter:item.nav"
        ng-href="{{key}}">
            {{item.label}}
    </a>
</section>

Warning: I haven't actually tested this code.警告:我还没有真正测试过这段代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM