我有一个从外部来源检索数据的工厂。一旦我得到数据,我就使用第二个工厂按特定标准对其进行过滤。
工厂属性分配给范围。
现在,当我在工厂执行此操作时,它不会更新范围:
factory.foo = [{id:1,name:'foo'}]; // doesn't work
因此第二家工厂的过滤器也不起作用
factory.foo = Filter.filter(); // doesn't work
虽然这有效:
factory.foo.push({id:1,name:'foo'}); // works
有谁知道这是否是有意的,为什么会这样,以及如何解决?
app.factory('Foo',function(Filter) {
var factory = {
foo:[],
getDataForFoo:function() {
factory.foo = Filter.filter(); // doesn't work
//factory.foo = [{id:1,name:'foo'},{id:1,name:'foo'}]; // doesn't work
//factory.foo.push({id:1,name:'foo'}); // works
}
};
return factory;
});
app.factory('Filter',function() {
var factory = {
filter:function() {
var arr = [];
arr.push({id:1,name:'foo'});
return arr;
}
}
return factory;
});
app.controller('MainCtrl', function($scope,Foo) {
$scope.test = 'running';
$scope.foo = Foo.foo;
$scope.click = Foo.getDataForFoo;
});