gpt4 book ai didi

javascript - 如何使用 angular2 更新编辑后的切换

转载 作者:行者123 更新时间:2023-12-03 02:06:53 25 4
gpt4 key购买 nike

我有一个新闻通讯订阅,最初它是根据用户登录时收到的新闻通讯设置的。

当我切换时事通讯时,它给了我成功更新的消息,但时事通讯仍然设置为 false,即使我已将其设置为 true。谁能帮我解决一下。

HTML:

<div class="col-lg-4 col-md-4 col-sm-4 col-xs-12 text-center">
<span style="font-weight: 100;">Newsletter{{company.newsletter|json}}</span>
<ul class="toggle">
<li>
<mat-slide-toggle class="showToggle" name="subscribe" [(ngModel)]="company.newsletter" #slider required (click)="openPopup($event,company)">
</mat-slide-toggle>
</li>
</ul>
</div>

T:

**Checklogin:**
this.ApiService
.checklogin()
.subscribe(
user => {
this.company= user.data[0];
}, error => {
console.log(error);
});

**newsletter toggle**
openPopup(event,company) {
var userData:any = {
_id: company._id,
email: company.email,
active: company.active,
newsletter:company.newsletter
};
this.ApiService
.editUserToggle(userData._id,userData)
.subscribe(
user => {
console.log(user);
this.toasterService.pop('success', 'User subscribed Successfully');
}, error => {
if(error.data && error.data.length > 0) {
this.toasterService.pop('error', error.data);
} else {
this.toasterService.pop('error', 'Something went wrong!');
}
})
}

即使我收到成功消息后,时事通讯仍然是错误的

最佳答案

您可能会从 update api 收到成功消息,但它与您发送的值相同,因此您总是会收到 false,

尝试更改新闻通讯值,

var userData:any = { newsletter: !company.newsletter };

openPopup(event,company) {
console.log(company);
if(!this.loggedIn){
event.preventDefault();
this.signIn.show();
}
var userData:any = {
_id: company._id,
email: company.email,
active: company.active,
newsletter: !company.newsletter
};
console.log(userData);
this.ApiService
.editUserToggle(userData._id,userData)
.subscribe(
user => {
console.log(user);
this.toasterService.pop('success', 'User subscribed Successfully');
this.newsletter = userData.newsletter;
}, error => {
if(error.data && error.data.length > 0) {
this.toasterService.pop('error', error.data);
} else {
this.toasterService.pop('error', 'Something went wrong!');
}
})
}

但是你仍然没有改变模型值,所以你应该做的是,在收到成功消息后,添加

this.newsletter = userData.newsletter;

关于javascript - 如何使用 angular2 更新编辑后的切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49767562/

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