如何更改过滤器内的$ scope变量

时间:2013-06-14 20:39:55

标签: angularjs

我需要在过滤器中更改$scope变量。 $scope变量用于ng-show属性,并且信息仅在过滤器中加入,因为我有ng-repeat一些信息并由某些过滤器应用,我需要知道何时过滤器删除我的所有结果以显示消息...这是一个例子:(这只是一个想法)

.controller("thing", function() {
   $scope.showText = false;
})

.filter("filterText", function() {
   return function(information) {
     if (information == "") { /* NEED TO CHANGE $scope.showText to true */ }
   }
})

HTML:

<div ng-view="showText"> Some Text here </div>
<div ng-repeat="info in information | filterText"></div>

感谢。

2 个答案:

答案 0 :(得分:4)

我同意这些评论,你可能不希望首先更改过滤器中的数据,但如果你真的很难按,你可以通过在控制器内定义过滤器功能来实现这一点(而不是一个实际的角度“过滤器”)然后只是使用它:

ng-repeat="item in items | filter:myFilter()"

$scope.myFilter = function(item) {
    // access to scope here
}

答案 1 :(得分:0)

Jeff的代码有一个小错误:我们只需要传递函数名称即可进行过滤

ng-repeat="item in items | filter:myFilter"

$scope.myFilter = function(item) {
    // access to scope here
}
相关问题