过滤器不工作从资源阵列 [英] Filters not working on array from resource

查看:253
本文介绍了过滤器不工作从资源阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,当它从工厂的阵列上运行没有返回任何一个滤波器。但是,当我复制阵列直接粘贴到过滤器,它工作正常。必须有一个简单的解决方案,它是推动我疯了。

本作品:

  $过滤器('过滤器')([
  {名:与firstItem,code:一},
  {名:secondItem,code:二},
  {名:thirdItem,code:三}
],两节,真正的);

这不:

  $过滤器('过滤器')($ scope.items,两节,真正的);

角示例:

  angular.module('应用',['ngResource']).controller(Ctrl键,功能($范围,$过滤器,邮件){
  $ scope.items = Items.query();
  变量codeToFilter =两化;
  $ scope.badFilter = $过滤器('过滤器')($ scope.items,codeToFilter,真正的);
  $ scope.goodFilter = $过滤器('过滤器')([
    {名:与firstItem,code:一},
    {名:secondItem,code:二},
    {名:thirdItem,code:三}
  ],两节,真正的);
}).factory(项目,函数($资源){
    返回$资源(项-list.asp);
});

和数组从项目-list.asp返回:

<$p$p><$c$c>[{\"name\":\"firstItem\",\"$c$c\":\"one\"},{\"name\":\"secondItem\",\"$c$c\":\"two\"},{\"name\":\"thirdItem\",\"$c$c\":\"three\"}]

这是我在页面上看到:

 坏过滤器:[]
良好的过滤器:[{名:secondItem,code:二}]


解决方案

Items.query()是异步,因此无法立即解决。在你的过滤器被击中的时候,它不填充。

设置它是这样的:

  Items.query(功能(结果){
    $ scope.items =结果;
    $ scope.badFilter = $过滤器('过滤器')($ scope.items,codeToFilter,真正的);
});

I have a filter that is not returning anything when it is run on an array from a factory. But when I copy paste the array directly into the filter, it works fine. There must be a simple solution, and it is driving me crazy.

This works:

$filter('filter')([
  {"name":"firstItem","code":"one"},
  {"name":"secondItem","code":"two"},
  {"name":"thirdItem","code":"three"}
],"two",true);

This doesn't:

$filter('filter')($scope.items,"two",true);

Angular sample:

angular.module('App', ['ngResource'])

.controller('Ctrl', function($scope, $filter, Items) {
  $scope.items = Items.query();
  var codeToFilter = "two";
  $scope.badFilter = $filter('filter')($scope.items,codeToFilter,true);
  $scope.goodFilter = $filter('filter')([
    {"name":"firstItem","code":"one"},
    {"name":"secondItem","code":"two"},
    {"name":"thirdItem","code":"three"}
  ],"two",true);
})

.factory("Items", function ($resource) {
    return $resource("item-list.asp");
});

And the array returned from item-list.asp:

[{"name":"firstItem","code":"one"},{"name":"secondItem","code":"two"},{"name":"thirdItem","code":"three"}]

This is what I see on the page:

Bad Filter: []
Good Filter: [{"name":"secondItem","code":"two"}]

解决方案

Items.query() is async and hence not resolved instantly. At the time your filter is hit, it's not populated.

Set it up like this:

Items.query(function(result) {
    $scope.items = result;
    $scope.badFilter = $filter('filter')($scope.items,codeToFilter,true);
});

这篇关于过滤器不工作从资源阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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