我建立了一个工厂,将信息保存在我的示波器中,共6页。现在,当用户完成第6页并推送对象时,我希望工厂重置为空数组。
我已经尝试了很多超时和应用元素,还尝试了很多组合将数组设置为空(null
,""
,{}
)。但是当我再次加载页面时它仍会加载旧信息(第1/6页)。
提交功能(还需要重置范围)是
$scope.send = function(){
if(ArrayInfo.Checkmark == true){
firebase.database().ref('lorem/' + ArrayInfo.Ordernumber).set({
info: ArrayInfo,
Dates: Dates,
gasmeter: gasmeter,
gasmeter1: gasmeter1
}).then(function(){
firebase.database().ref('lorem2/' + ArrayInfo.currentYear).set({
last_number: ArrayInfo.Ordervalue
});
}).then(function(){
//ArrayInfo = {};
setTimeout(function(){
ArrayInfo = "";
$scope.info = "";
$scope.$apply();
$scope.$digest();
}, 50);
});
//close newrental
setTimeout(function(){
if (window.confirm('Information saved! You are ready to leave this screen? no changes possible after this point'))
{
//disable back button in home
$ionicHistory.nextViewOptions({
disableBack: true
});
//go home
$state.go("app.auth");
}
//error close newrental
else
{
alert("Take your time");
}
}, 50);
}
//error send array
else {
alert("Please accept the terms and conditions.");
}
}
我的工厂看起来像这样
mainapp.factory("infoFactory", function(){
ArrayInfo = {};
placeholders = {
"licenseone" : "img/placeholder.png",
"licensetwo" : "img/placeholder.png",
"licensethree" : "img/placeholder.png",
"licensefour" : "img/placeholder.png",
"imageone" : "img/front.png",
"imagetwo" : "img/sideleft.png",
"imagethree" : "img/back.png",
"imagefour" : "img/sideright.png",
"imagefive" : "img/roof.png",
"imagesix" : "img/placeholder.png",
"imageseven" : "img/placeholder.png",
"imageeight" : "img/placeholder.png"
};
gasmeter = {
"url" : "img/gas/gas1.png",
"gasvalue" : "1"
}
gasmeter1 = {
"url" : "img/gas/gas1.png",
"gasvalue" : "1"
}
ArrayInfo.returned = false;
RawDate = {};
Dates = {};
console.log(ArrayInfo);
return ArrayInfo;
return gasmeter;
return gasmeter1;
return placeholders;
return RawDate;
return Dates;
})
我将信息加载到我的控制器中
$scope.info = infoFactory;
$scope.Dates = Dates;
$scope.RawDate = RawDate;
$scope.gasmeter = gasmeter;
$scope.gasmeter1 = gasmeter1;
我使用的角度版本是" 3.6.6"
答案 0 :(得分:1)
首先,当您在代码中放置return
时,之后不再包含其他代码,因为它永远不会运行。您需要返回Object
。
mainapp.factory("infoFactory", function(){
ArrayInfo = {};
placeholders = {
"licenseone" : "img/placeholder.png",
// Rest of the images
};
gasmeter = {
"url" : "img/gas/gas1.png",
"gasvalue" : "1"
}
gasmeter1 = {
"url" : "img/gas/gas1.png",
"gasvalue" : "1"
}
ArrayInfo.returned = false;
RawDate = {};
Dates = {};
console.log(ArrayInfo);
return {
ArrayInfo: ArrayInfo,
gasmeter: gasmeter,
gasmeter1: gasmeter1,
placeholders: placeholders,
RawDate: RawDate,
Dates: Dates
};
})
现在您可以将infoFactory
注入控制器并使用它:infoFactory.RawDate
。
现在,如果要重置工厂,可以添加一个重置所有数据的功能:
mainapp.factory("infoFactory", function(){
// Save a reference to the current pointer of the factory, so you won't loose it inside other scopes
var self = this;
self.params = {
ArrayInfo: {},
placeholders: {},
gasmeter: {},
gasmeter1: {},
ArrayInfo: false,
RawDate: {},
Dates: {}
};
self.reset = function() {
self.params.ArrayInfo = {};
self.params.placeholders.licenseone = "img/placeholder.png";
self.params.gasmeter.url = "img/gas/gas1.png";
self.params.gasmeter.gasvalue = "1";
self.params.gasmeter1.url = "img/gas/gas1.png";
self.params.gasmeter1.gasvalue = "1";
self.params.ArrayInfo.returned = false;
self.params.RawDate = {};
self.params.Dates = {};
}
self.reset(); // Call this function by default in order to initially set the factory properties
return {
reset: self.reset, // You can export the reset function and use it outside the factory too
ArrayInfo: self.params.ArrayInfo,
gasmeter: self.params.gasmeter,
gasmeter1: self.params.gasmeter1,
placeholders: self.params.placeholders,
RawDate: self.params.RawDate,
Dates: self.params.Dates
};
})
现在,当你有一个重置功能时,只要你想将数据重置为初始状态,就可以像工厂的{strong> 那样使用它:infoFactory.reset()
。我在工厂内部创建了一个基础对象(this.params = { .. }
)并在其中保存了所有细节属性,在reset
函数内部我更新了这些属性而没有打破原始引用( Working example)。
以上只是一个示例,但您可以(或者应该)封装工厂的params
,并且只允许用户通过帮助函数控制和更改值。如何操作的示例:
mainapp.factory("infoFactory", function(){
var self = this;
self.params = {
returned: false,
};
return {
setReturned: function(val) { self.params.returned = val === true; },
returned: function() { return self.params.returned; }
}
});
上面的示例将隐藏来自工厂外的用户的实际params.returned
,并且只允许它通过帮助函数设置返回的标志,即infoFactory.setReturned( true );
或infoFactory.setReturned( false );
,在setReturned
函数内部,您可以实现复杂的逻辑来验证发送给函数的值。请注意,infoFactory.setReturned( 'invalid value!!!' );
会将returned
标记设置为false
,因为我使用strict === operator - val === true
验证了该值。
然后,要从工厂获取值,请调用infoFactory.returned()
函数 - 通过使用函数阻止外部访问工厂属性。
作为旁注 - 请勿使用setTimeout(function(){ ... });
使用$timeout和$interval,然后您不需要$scope.$apply();
+ $scope.$digest();
才能手动运行一个摘要周期,因为它是由Angularjs为你自己管理的