2017-08-25 123 views
0

我希望能够在带有过滤器的另一个变量上构建的Angular模板中引用变量。基于另一个变量的AngularJS引用变量

因此,例如,我可能有这样的控制器:

$ scope.EuropaLeague = TRUE;

如果我这样做是它的工作原理模板预期:

<div ng-if="EuropaLeague"> 
 

 
</div>

但如果我想动态填充NG-如果有东西从未来的NG-重复例如

{{item.leagueName | myFilter}}

所以上述引用我的范围变量$ scope.EuropaLeague例如对或错?

感谢

+0

是什么问题? –

+0

问题是ng-if“{{item.leagueName | myFilter}}”不起作用。它只能输出字符串'EuropaLeague'。我想引用EuropaLeague范围变量。 –

+0

你可以提供一个plunkar –

回答

2

这里是工作代码:

var app = angular.module('plunker', []); 
 

 
app.controller('MainCtrl', function($scope) { 
 
    $scope.leagues = 
 
    [ 
 
    { 
 
     "id": 1, 
 
     "name": "ChampionsLeague", 
 
    }, 
 
    { 
 
     "id": 2, 
 
     "name": "EuropaLeague", 
 
    }, 
 
    { 
 
     "id": 3, 
 
     "name": "FACup", 
 
    } 
 
    ] 
 
    $scope.obj = {}; 
 
    $scope.obj['EuropaLeague'] = false; 
 
    $scope.obj['FACup'] = false; 
 
    $scope.obj['ChampionsLeague'] = true; 
 
    
 
});
<!DOCTYPE html> 
 
<html ng-app="plunker"> 
 

 
    <head> 
 
    <meta charset="utf-8" /> 
 
    <title>AngularJS Plunker</title> 
 
    <script>document.write('<base href="' + document.location + '" />');</script> 
 
    <link rel="stylesheet" href="style.css" /> 
 
    <script data-require="[email protected]" src="https://code.angularjs.org/1.3.20/angular.js" data-semver="1.3.20"></script> 
 
    <script src="app.js"></script> 
 
    </head> 
 

 
    <body ng-controller="MainCtrl"> 
 
    <ul ng-repeat="item in leagues" ng-if="item.name"> 
 
     <li ng-if="obj[item.name]">{{item.name}}</li> 
 
    </ul> 
 
    </body> 
 

 
</html>

+0

太棒了!谢谢。 –

+0

@NeilSingh很乐意帮忙:-) –