不更新的数组变量$角由过滤器$资源返回 [英] Angular $filter not updating variable on array returned by $resource

查看:211
本文介绍了不更新的数组变量$角由过滤器$资源返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个角 $资源返回的数组,它们有一个名称属性。我想用 $过滤器 在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆