2016-09-08 71 views
0

我尋找隱藏的類別,如果它是空的。我目前的解決方案是行不通的:隱藏空過濾NG重複

HTML:

<ul> 
    <li data-ng-repeat="items in shop | unique : 'cat' | orderBy: 'cat'"> 
     <h2>{{items.cat}}</h2> 
     <ul> 
      <li ng-repeat="items in shop | filter:{cat: items.cat} | filter:query" 
       <h3>{{items.name}}</h3> 
      </li> 
     </ul> 
    </li> 
</ul> 

我的過濾器 「獨特的」 看起來像這樣:

app.filter('unique', function() { 
    return function(collection, keyname) { 
     var output = [], 
      keys = []; 

     angular.forEach(collection, function(item) { 
      var key = item[keyname]; 
      if(keys.indexOf(key) === -1) { 
       keys.push(key); 
       output.push(item); 
      } 
     }); 

     return output; 
    }; 
}); 

JSON:

{ 
    "businesses" : { 
    "-KQ1ggyrnYGYL1084Htz" : { 
     "name" : "business name", 
     "cat" : "food", 
    }, 
} 

我認爲我可以用ng-if和/或ng-hide/show做一些事情,但是我沒有做過任何組合工作。

+0

你能告訴你的JSON? – cnorthfield

+0

是的,我更新了JSON,我很抱歉沒有事先。而且我認爲你的答案可行,但我正在考慮爲頂級的重複做。這是一個嵌套的ng-repeat。該解決方案適用於兒童。但我正在尋找隱藏父母「貓」標籤如果是空的。感謝 – Auzy

+0

看到我更新的答案 – cnorthfield

回答

0

您可以使用ngIf指令或ngShow/ngHide指令。

如何使用它們取決於你的JSON。

繼承人的ngIf的一些基本的數據爲例:

(function() { 
 

 
    angular.module('MyApp', []); 
 

 
})(); 
 

 
(function() { 
 

 
    angular.module('MyApp').controller('MyController', MyController); 
 

 
    MyController.$inject = ['$scope']; 
 

 
    function MyController($scope) { 
 

 
    $scope.shop = [{ 
 
     name: "test 1", 
 
     cat: "cat 1" 
 
    }, { 
 
     name: "test 2", 
 
     cat: "cat 3" 
 
    }, { 
 
     name: "test 3", 
 
     cat: "" 
 
    }, { 
 
     name: "test 4", 
 
     cat: "cat 4" 
 
    }]; 
 

 
    } 
 

 
})();
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 

 
<div data-ng-app="MyApp"> 
 

 
    <div data-ng-controller="MyController"> 
 

 
    <ul> 
 
     <li data-ng-repeat="items in shop | orderBy: 'cat'" ng-if="items.cat"> 
 
     <h2>{{items.cat}}</h2> 
 
     <ul> 
 
      <li ng-repeat="items in shop | filter:{cat: items.cat}"> 
 
      <h3>{{items.name}}</h3> 
 
      </li> 
 
     </ul> 
 
     </li> 
 
    </ul> 
 

 
    </div> 
 

 
</div>