我正在尝试从http请求中应用过滤器。
我有类似的东西
html
<div ng-repeat="product in products | testFilter:5 "> {{product.title}} </div>
js
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
}
})
过滤器的效果为“ AFTER”,我从中获取了数据myService
。但是,在加载数据的同时,我一直在获取'Cannot read property 'length' of undefined'
。我知道是因为数据仍在加载。我该如何解决这个问题?谢谢。
您只需要在进行ajax调用之前初始化$ scope.products。
像:$scope.products = [];
应该做!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句