请帮帮忙。我使用ngStorage
来存储一些全局变量:AngularJS 1.6.1,TypeScript,UI Bootstrap Modal:为什么我的全局变量存储在会话存储中,并被模态更新?
//Global Controller
saveGlobalSettings =() => {
this.$sessionStorage.QuoteTimes = this.quoteTimes;
this.$sessionStorage.QuoteTypes = this.quoteTypes;
};
我用了另一个控制器的另一种方法来创建使用一些全局变量作为属性的对象:
// Create Controller
addChannel = (channel) => {
channel.QuotingChannelType = channel.QuotingChannelId;
console.log(this.$sessionStorage.QuoteTimes);
channel.QuoteTypes = this.$sessionStorage.QuoteTypes;
channel.QuoteTimes = this.$sessionStorage.QuoteTimes;
if (!this.myChannels) { this.myChannels = []; }
this.myChannels.push(channel);
};
我然后使用ng-repeat
在浏览器中显示myChannels
中的对象。单击时,我传递的对象的openDialog()
方法:
openPreferencesDialog = (channel) => {
var options: ng.ui.bootstrap.IModalSettings = {
templateUrl: 'app/form/templates/editChannelPreferences.html',
controller: 'EditDialogController',
controllerAs: '$ctrl',
resolve: {
channel:() => channel
}
};
this.$uibModal.open(options).result
.then(updatedChannel => console.log(updatedChannel));
};
的对话框打开,如预期的,但是当我做出改变,它是在$sessionStorage
(从console.log
在addChannel
看到更新QuoteTimes
和QuoteTypes
阵列)。因此,创建的每个新对象都有我编辑的最后一个对象的QuoteTimes
和QuoteTypes
。我完全难倒了。我最好的猜测是这是一种范围链问题?任何想法我做错了什么?
UPDATE:把它按预期工作,我能够通过使用JSON.parse(JSON.stringify(obj));
如下:
openPreferencesDialog = (obj, index) => {
var channel = JSON.parse(JSON.stringify(obj));
var options: ng.ui.bootstrap.IModalSettings = {
templateUrl: 'app/form/templates/editChannelPreferences.html',
controller: 'EditDialogController',
controllerAs: '$ctrl',
resolve: {
channel:() => channel
}
};
this.$uibModal.open(options).result
.then(updatedChannel =>
{
console.log(updatedChannel);
this.$sessionStorage.myChannels[index].QuoteTimes = updatedChannel.QuoteTimes;
this.$sessionStorage.myChannels[index].QuoteTypes = updatedChannel.QuoteTypes;
console.log(this.$sessionStorage.myChannels)
});
};
任何人都可以解释为什么这工作?