2016-05-12 79 views
0

我想通過變量提供過濾器值。我試圖包含模型引用{{fil}}的ng-repeat行出現了什麼問題?AngularJS過濾器語法

<!DOCTYPE html> 
<html> 
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
<body> 

<div ng-app="myApp" ng-controller="namesCtrl"> 

<ul> 
    <li ng-repeat="x in names | filter : {{fil}}"> 
    {{ x }} 
    </li> 
</ul> 

<p>This example displays only the names containing the letter {{ fil }}.</p> 

</div> 

<script> 
angular.module('myApp', []).controller('namesCtrl', function($scope) { 
    $scope.names = [ 
     'Jani', 
     'Carl', 
     'Margareth', 
     'Hege', 
     'Joe', 
     'Gustav', 
     'Birgit', 
     'Mary', 
     'Kai' 
    ]; 
    $scope.fil = "a"; 
}); 
</script> 

</body> 
</html> 

回答

1

你應該通過它無需插值{{}},所以它會應用過濾器fil這是有範圍的。

<li ng-repeat="x in names | filter : fil"> 
    {{ x }} 
</li>