过滤控制器中的数据

时间:2014-03-18 12:20:50

标签: angularjs

我正在尝试过滤控制器内部的数据:

MyApp.controller( 'MyCtrl', [ '$scope', '$filter', function( $scope, $filter )
{
    $scope.elements = // array data;

    $scope.filterByUserSetting = function( element )
    {
        // return true/false depending on whether filter criterion matches
        // this part is fine
    }

     $scope.elementsFiltered = $filter("filterByUserSetting")($scope.elements);
    // this part doesn't work
}]);

我得到的错误信息是:

Error: [$injector:unpr] http://errors.angularjs.org/1.2.12/$injector/unpr?p0=filterByUserSettingFilterProvider%20%3C-NaNilterByUserSettingFilter at Error (native)...

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您需要定义过滤器提供程序,例如

angular.module('MyApp', []).
    filter('filterByUserSetting', function() {
      return function(input) {
        // your return, based on your filter logic
      };
    });

然后在你的控制器中使用它

$scope.elementsFiltered = $filter( "filterByUserSetting" )( $scope.elements )