admin管理员组文章数量:1415484
<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
2 Answers
Reset to default 5in 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)"
本文标签: javascriptDynamic property inside ngif statementStack Overflow
版权声明:本文标题:javascript - Dynamic property inside ng-if statement - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745232913a2648906.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论