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"}]