How to use ng-if with ng-repeat?

后端 未结 5 1039
北恋
北恋 2020-11-30 13:40

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,

5条回答
  •  [愿得一人]
    2020-11-30 14:00

    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. I'll push that up to github sometime soon, but it's kludgy.

    The simpler way to do it is to modify your route.routes collection (or create a placeholder collection)

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

    and in your view

    ...
    

    If you need it to be dynamically updated, just set up watches, etc.

提交回复
热议问题