javascript - Dynamic property inside ng-if statement - Stack Overflow

<ul><li ng-repeat="category in categoryList">{{category.Name}} <br><u

<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>

<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>

I want to check if a certain game has a child with a name of the category, with the brackets it would just look up a specific child called category which doesn't exist.

Share Improve this question edited Jan 1, 2017 at 7:17 jophab 5,52714 gold badges44 silver badges61 bronze badges asked Dec 17, 2014 at 13:43 DorgalamDorgalam 351 silver badge6 bronze badges 5
  • try to change it to ng-if="game[{{category.Name}}]" – Rasalom Commented Dec 17, 2014 at 13:45
  • 1 methinks better ng-if="game[category.Name]" – Grundy Commented Dec 17, 2014 at 13:48
  • @user4370437 so which ment is work? :-) – Grundy Commented Dec 17, 2014 at 13:53
  • I think yours, or both :D – Rasalom Commented Dec 17, 2014 at 13:54
  • 1 @Rasalom small fiddle :-D – Grundy Commented Dec 17, 2014 at 14:05
Add a ment  | 

2 Answers 2

Reset to default 5

in ng-if directive you can reference to variable category directly

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./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>

You may have to do that from a function in your controller. $scope.nameExists = function (game, name){return typeof game[name] !== 'undefined'; }; then ng-if="nameExists (game, category.Name)"

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745232914a4617760.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信