【发布时间】:2014-12-07 21:48:26
【问题描述】:
我一直在努力正确实现这一点,我以为我做到了,但根据示例我做错了。你如何在控制器中使用这个工厂?预期的结果是什么?
例子:
app.factory("ListWithTotal", ["$FirebaseArray", "$firebase", function($FirebaseArray, $firebase) {
// create a new factory based on $FirebaseArray
var TotalFactory = $FirebaseArray.$extendFactory({
getTotal: function() {
var total = 0;
// the array data is located in this.$list
angular.forEach(this.$list, function(rec) {
total += rec.amount;
});
return total;
}
});
return function(listRef) {
// override the factory used by $firebase
**// why do listRef and ref not match here?
//where does listRef or ref come from**
var sync = $firebase(ref, {arrayFactory: TotalFactory});
return sync.$asArray(); // this will be an instance of TotalFactory
}
}]);
我已经能够使用不包括返回对新 Firebase 的引用的扩展工厂版本。例如:一个保持计数的待办事项列表:http://plnkr.co/edit/iAGvPHFWn2GSPGzBRpKh?p=preview
【问题讨论】:
-
这是一个错字。它们都应该是
listRef或ref。不是一个和另一个。
标签: javascript angularjs firebase angularfire