gpt4 book ai didi

javascript - 了解 AngularFire 扩展 FirebaseArray 示例?

转载 作者:行者123 更新时间:2023-11-28 00:46:16 24 4
gpt4 key购买 nike

我一直在努力正确实现这一点,我以为我已经做到了,但根据这个例子,我做错了。如何在 Controller 中使用这个工厂?预期的结果是什么?

示例:

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

最佳答案

这是一个错字。它们都应该是 listRefref。没有一个和另一个。

当 ListWithTotal 工厂返回一个函数时,您可以在 Controller 中使用它,如下所示:

.controller('MyController', function ($scope, $window, ListWithTotal) {
var ref = new $window.Firebase("//some-url/data");
$scope.listWithTotal = ListWithTotal(ref);
});

关于javascript - 了解 AngularFire 扩展 FirebaseArray 示例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27348238/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com