gpt4 book ai didi

javascript - Angularfire $firebaseArray 扩展动态计数\总和

转载 作者:行者123 更新时间:2023-12-03 03:23:46 26 4
gpt4 key购买 nike

我正在尝试实现某个节点的动态计数(如果已定义):

这可以工作,但它不是动态的,你必须调用 sum()。

app.factory("ArrayWithSum", function($firebaseArray) {
return $firebaseArray.$extend({
sum: function() {
var total = 0;
var todayDate = new Date();
var start = todayDate.setHours(0,0,0,0);
var end = todayDate.setHours(23,59,59,999);

// the array data is located in this.$list
angular.forEach(this.$list, function(rec) {
if (angular.isDefined(rec.qa)){
if (angular.isDefined(rec.qa.completed)) {
if (rec.qa.completed >= start && rec.qa.completed <= end){
total++;
}
}
}

});
return total;
}
});
});

我尝试了 $$update 但无法访问数组中的 this_counter:

  app.factory("counter", function($firebaseArray) {
return $firebaseArray.$extend({
sum: function() {
return this._counter;
},
$$updated: function(){

var changed = $firebaseArray.prototype.$$updated.apply(this, arguments);

var todayDate = new Date();
var start = todayDate.setHours(0,0,0,0);
var end = todayDate.setHours(23,59,59,999);

if( !this._counter ) {
this._counter = 0;
}

// the array data is located in this.$list
angular.forEach(this.$list, function(rec) {
if (angular.isDefined(rec.qa)){
if (angular.isDefined(rec.qa.completed)) {
if (rec.qa.completed >= start && rec.qa.completed <= end){
this._counter++;
}
}
}

});
return changed;
}
});
});

有谁知道如何创建一个可以更新和访问的动态变量?

谢谢

最佳答案

让它与 $firebaseObject 一起工作。显然,您可以将属性 _counter 添加到对象而不是数组。不管怎样,这都是获得动态计数的好方法。

app.factory("counter", function($firebaseObject) {
return $firebaseObject.$extend({
$$updated: function(){

var changed = $firebaseObject.prototype.$$updated.apply(this, arguments);

if( !this._counter ) { this._counter = 0; }

var total = 0;
var todayDate = new Date();
var start = todayDate.setHours(0,0,0,0);
var end = todayDate.setHours(23,59,59,999);

// the array data is located in this.$list
angular.forEach(this, function(rec) {
if (angular.isDefined(rec.qa)){
if (angular.isDefined(rec.qa.completed)) {
if (rec.qa.completed >= start && rec.qa.completed <= end){
total++;
}
}
}

});

this._counter = total;
return changed;
}
});
});


vm.panels = new counter(panelsRef);

{{vm.panels._counter}}

我遇到了 watch 在 IE11 上长时间运行后不触发的问题。所以我想尝试这种方法。

关于javascript - Angularfire $firebaseArray 扩展动态计数\总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46444972/

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