【问题标题】:how to check whether a filter in angular js has returned any data or not in the search如何检查angular js中的过滤器是否在搜索中返回了任何数据
【发布时间】:2015-02-28 12:42:40
【问题描述】:

我对angular js很陌生。我想知道如何在angular js中调用基于过滤器搜索的函数。当基于过滤器的搜索没有返回值时,我需要在控制器中调用一个函数。

<li ng-repeat="test in test_list | filter: test_id">
                    {{ test}} 
                 </li>

当 {{test}} 的列表为空时,我想在控制器中调用一个函数。

这对你们来说可能很容易。在此先感谢。

【问题讨论】:

  • 观察过滤后的值:$scope.$watch('test_list | filter: test_id', function (filteredList) { if (filteredList.length === 0) { doWhateverYouWantHere(); } })
  • 您能否详细说明一下,并提及如何在视图中使用它。提前致谢
  • 那没用..这个问题是我只有维奈发布的:)

标签: angularjs filter


【解决方案1】:
<li ng-repeat="test in results = (test_list | filter: test_id)">
                    {{ test}} 
                 </li>

然后您将能够访问 $scope.results.length

如果需要在没有结果的时候显示消息添加

<li ng-show="results.length == 0">No results</li>

如果你想检查控制器中的长度,有几种方法可以做到......这只有两种......

<input type="text" id="test_id" ng-model="test_id" ng-change="controllerOnChangeFunction()"/>

在你的控制器中

$scope.controllerOnChangeFunction = function() {
    if ($scope.results.length == 0) {
         //do stuff here
    }
}

或者,在您的控制器中(请记住,此手表的评估频率也会超出您的需要。

$scope.watch(
    function(){
        return $scope.results.length
    }, 
    function(newVal){
        if (newVal == 0) { 
             // do you stuff here
        }
    }
);

【讨论】:

  • 那么如何调用函数呢?如果结果为零?
  • 同样的问题...如果结果为零,如何调用函数?
  • test in results = (test_list | filter: test_id) 成功了,谢谢我不知道!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-19
  • 2017-04-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多