Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在http请求中应用自定义筛选器?_Javascript_Html_Angularjs - Fatal编程技术网

Javascript 如何在http请求中应用自定义筛选器?

Javascript 如何在http请求中应用自定义筛选器?,javascript,html,angularjs,Javascript,Html,Angularjs,我正在尝试从http请求应用筛选器 我有点像 html 过滤器在我从myService获取数据后“工作”。但是,在加载数据时,我不断得到'cannotreadproperty'length'的undefined'。我知道这是因为数据还在加载。我如何解决这个问题?谢谢。在进行ajax调用之前,只需初始化$scope.products即可 比如: $scope.products=[] 应该这样做 将$scope.products初始化为空数组[],然后在加载数据时对其进行更新。 <div ng

我正在尝试从http请求应用筛选器

我有点像

html


过滤器在我从
myService
获取数据后“工作”。但是,在加载数据时,我不断得到
'cannotreadproperty'length'的undefined'
。我知道这是因为数据还在加载。我如何解决这个问题?谢谢。

在进行ajax调用之前,只需初始化$scope.products即可

比如:
$scope.products=[]

应该这样做

$scope.products
初始化为空数组
[]
,然后在加载数据时对其进行更新。
<div ng-repeat="product in products | testFilter:5 "> {{product.title}} </div>
app.controller('test', function($scope, $filter){
   myService.get('api/v1/products/?id=1').success(function(data) {
       $scope.products = data;    

   })
}

app.filter('testFilter', function() {
      return function(products, item) {             
          //codes...
          var t = products.length
      }
})