Javascript 理解AngularFire扩展FirebaseArray示例?
我一直在努力实现这一点,我认为我已经做到了,但基于这个例子,我做错了。如何在控制器中使用此工厂?预期的结果是什么 例如:Javascript 理解AngularFire扩展FirebaseArray示例?,javascript,angularjs,firebase,angularfire,Javascript,Angularjs,Firebase,Angularfire,我一直在努力实现这一点,我认为我已经做到了,但基于这个例子,我做错了。如何在控制器中使用此工厂?预期的结果是什么 例如: app.factory("ListWithTotal", ["$FirebaseArray", "$firebase", function($FirebaseArray, $firebase) { // create a new factory based on $FirebaseArray var TotalFactory = $FirebaseArray.$ext
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的引用。例如:保持计数的待办事项列表:这是一个打字错误。它们都应该是
listRef
,或者ref
。不是一个和另一个
当ListWithTotal工厂返回一个函数时,您可以在控制器中使用它,如下所示:
.controller('MyController', function ($scope, $window, ListWithTotal) {
var ref = new $window.Firebase("//some-url/data");
$scope.listWithTotal = ListWithTotal(ref);
});
这是个打字错误。它们都应该是
listRef
,或者ref
。不是一个和另一个。很酷,所以它取代了$firebase函数。有必要在那里设置美元窗口吗?我通常只写var ref=newfirebase(“/path to Firebase”);由于$window
是Angular对window对象的包装,全局对象的任何属性(如Firebase
)也可在$window
上找到。有了代码引用$window
,您就可以轻松地在测试中模拟全局范围内的内容。