【问题标题】:Hide a ng-repeat, if a child/nested repeat has a value [duplicate]如果子/嵌套重复具有值[重复],则隐藏 ng-repeat
【发布时间】:2013-12-26 23:46:12
【问题描述】:

我有多个 ng-repeats,它们相互依赖。

$scope.niveaus = [
        { niveau: 'a' },
        { niveau: 'b' },
        { niveau: 'c' },
        { niveau: 'd' },
        { niveau: 'e' },
        { niveau: 'f' },
        { niveau: 'g' }
    ];

    $http.get('/goals.json').success(function(goals) {
        $scope.goals = goals;
    });

我填写硬编码的 niveau A-G。在目标列表中,我将 niveaus 的名称与 JSON 中的名称相匹配。

然后做一个 ng-repeat:

<ul ng-repeat="niveau in niveaus">

        <li class="foobar">{{ niveau.niveau }}</li>

        <div ng-repeat="goal in goals | unique:'niveau_id'">
            <div ng-if="goal.niveau.name == niveau.niveau">
                <li class="yes">{{ goal.niveau.name }}</li> 
            </div>
        </div>

    </ul>

因此它会检查每个 niveau 的目标。如果目标与名称匹配,它将显示。 但是现在的问题是,如果 ng-if 中有匹配项,也会显示 li.foobar。

所以这可以是输出:

但它应该有唯一的 niveau 名称。 每一行的 A-G。并显示重复部分中是否存在 li.yes,它应该从重复列表中隐藏 li.foobar。

但是如何在 Angular 中实现呢?

【问题讨论】:

    标签: javascript angularjs angularjs-scope angularjs-ng-repeat


    【解决方案1】:

    查看filters上的文档

    angular.module('myApp', []).filter('withgoals', function() {
    return function(input, goals) {
      var out = [] 
      for (var i = 0; i < goals.length; i++) {
        if (input.niveau == goals[i].niveau.name) out.push(input[i])
      }
      return out;      
    });
    
    
    <div ng-repeat='n in niveaus| withgoals: goals'></div>
    

    【讨论】:

    • 那么你只会得到内部有目标的妮维雅。当内部没有目标时,我还想展示 niveau。每一行都应该有以下内容:A B C D E F G.
    • @user1469734 将示例数据结构和生成的 html 布局添加到您的问题中,因为不清楚,因为您没有显示 goal 部分数据。
    猜你喜欢
    • 2016-05-17
    • 2019-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多