[英]How to update the edited toggle using angular2
i have a newsletter subscription, initially it is set according to newsletter i get when the user login. 我有一个通讯订阅,最初是根据用户登录时收到的通讯设置的。
when i toggle the newsletter thing, it gives me succesfully updated message but the newsletter is still set to false even though i had made it to true. 当我切换时事通讯时,它会给我成功更新的消息,但即使我已将其设置为true,该时事通讯仍设置为false。 Can anyone help me to sort it out.
谁能帮我解决这个问题。
HTML: 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>
Ts: TS:
**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!');
}
})
}
Even after i get success message the newsletter will still be false 即使在我收到成功消息后,该新闻通讯仍然是错误的
You might be getting success message from the update api
, but it will be the same value what you send so you will always get false, 您可能会从
update api
获得成功消息,但它与您发送的消息的值相同,因此您总是会得到false,
try changing the newsletter value, 尝试更改新闻通讯的价值,
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!');
}
})
}
But still you didnt change the model value, so what you should do is, after you get the success message, add 但是您仍然没有更改模型值,因此您应该做的是,在获得成功消息后,添加
this.newsletter = userData.newsletter;
You are calling the service to update the news letter service but you are not toggling the company.newsletter ngModel value. 您正在调用该服务以更新新闻信服务,但是您没有切换company.newsletter ngModel值。 Try wth that
尝试一下
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!');
}
})
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.