不更新的数组变量$角由过滤器$资源返回 [英] Angular $filter not updating variable on array returned by $resource
问题描述
说我有一个角 $资源
返回项
的数组,它们有一个名称
属性。我想用 $过滤器
在Javascript中(不可以采用了棱角分明的 {{嗒嗒|过滤:过滤器}}
语法模板内)提取基于属性的条目的子集
Say I've got an Angular $resource
that returns an array of entries
that all have a name
attribute. I would like to use $filter
in Javascript (not using Angular's {{ blah | filter:filter }}
syntax within the templates) to extract a subset of those entries based on the attribute.
在code看起来是这样的:
The code would look something like:
app.controller('AppController', function ($scope, Project, $filter) {
$scope.entries = Project.query();
$scope.entry = $filter('filter')($scope.entries, {name: 'Bar'});
...
}
然而,在这种的jsfiddle说明这里,该片段将不会按预期进行。 输入
将包含一个空数组不管是什么项
获取与异步填充,在XHR完成时。建立一个 $范围。$看
也不起作用。
However, as illustrated in this jsFiddle here, the snippet won't work as intended. entry
will contain an empty array regardless of what entries
gets populated with asynchronously, when the XHR completes. Setting up a $scope.$watch
also doesn't work.
我想更新输入
根据过滤器时项
填充数据。我怎样才能做到这一点?
I want to update entry
according to the filter when entries
is populated with data. How can I do this?
推荐答案
好吧,我是pretty多少与我的问题后,当我尝试多一点,我发现我可以通过提供一个<$ C $做到这一点C>成功回调至查询()
电话:
Okay, I was pretty much done with my question when I experimented a bit more and found that I could do this by supplying a success
callback to the query()
call:
app.controller('AppController', function ($scope, Project, $filter) {
$scope.entries = Project.query({}, function() {
$scope.entry = $filter('filter')($scope.entries, {name: 'Bar'});
});
...
}
查看更新的jsfiddle 。
这篇关于不更新的数组变量$角由过滤器$资源返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!