在ng-if中获取$ scope.variable

时间:2016-09-11 07:23:29

标签: angularjs

我试图简单地使用像这样的$ scope.variable名称

<a href="index.html" ng-if="{{variableName}}"> <!-- angular.js:11706 Error: [$parse:syntax] Syntax Error: Token '{' invalid key -->

如何在ng-if语句中使用{{variableName}}?

2 个答案:

答案 0 :(得分:3)

替换为

 <a href="index.html" ng-if="variableName">

答案 1 :(得分:0)

使用方括号[]中的类别。

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>