如何使用angular2更新已编辑的切换

时间:2018-04-11 06:00:22

标签: javascript angular typescript

我订阅了新闻通讯,最初根据用户登录时收到的新闻通讯进行设置。

当我切换简报时,它给了我成功更新的消息,但即使我已将其设为真,新闻简报仍然设置为假。任何人都可以帮我解决。

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:

**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!');
            }
         })
  } 

即使在我获得成功消息后,通讯仍然是假的

2 个答案:

答案 0 :(得分:1)

您可能会从update api获得成功消息,但它与您发送的内容相同,因此您将始终获得错误,

尝试更改简报值,

<强> 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;

答案 1 :(得分:0)

您正在调用该服务来更新新闻信函服务,但您没有切换company.newsletter ngModel值。试试那个

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!');
        }
     })

}