ng-if语句中的动态属性

时间:2014-12-17 13:43:42

标签: javascript angularjs dom angularfire



<ul>
<li 
ng-repeat="category in categoryList">
{{category.Name}} <br>
<ul>
<li 
ng-repeat="game in gameList" ng-if="game.{{category.Name}}">
{{game.Name}}
</li>
</ul>
</li>
</ul>
&#13;
&#13;
&#13;

我想检查某个游戏是否有一个具有该类别名称的孩子,使用括号它只会查找一个名为“不存在的类别”的特定孩子。

2 个答案:

答案 0 :(得分:5)

ng-if指令中,您可以直接引用变量category

ng-if="game[category.Name]"

angular.module('app',[])
.controller('ctrl',function($scope){
    $scope.categoryList = [
        {Name : '1'},
        {Name : '2'},
        {Name : '3'},
        {Name : '4'},
        {Name : '5'}
    ];
    
    $scope.gameList = [
        {Name : 'g1', '1':true},
         {Name : 'g2', '1':true},
         {Name : 'g3', '2':true},
         {Name : 'g4', '7':true}
    ]
    
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="app" ng-controller="ctrl">
  <ul>
    <li ng-repeat="category in categoryList">
      Category - {{category.Name}} <br>
      <ul>
        <li ng-repeat="game in gameList" ng-if="game[category.Name]">
          Game - {{game.Name}}
        </li>
      </ul>
    </li>
  </ul>
</div>

答案 1 :(得分:0)

您可能必须从控制器中的功能执行此操作。 $scope.nameExists = function (game, name){return typeof game[name] !== 'undefined'; };然后ng-if="nameExists (game, category.Name)"