2017-08-25 122 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很樂意幫忙:-) –