0

我有一个名为 ngx-loading 的 npm 包。我必须设置 this.loading = true 和 this.loading = false 让它显示而不显示。当我在 javascript 中使用警报时,问题就来了。警报显示“ok”和“cancel”。当我点击“确定”时,加载器消失了,而如果我点击“取消”和“x”,它仍然出现?当我单击取消或 x 按钮时,如何使加载程序消失?这是我在下面所做的。

TS

onUpdate(form: NgForm) {
  this.loading = true;

  name =  form.value.name,

  if (confirm('Are you sure you want to update?')) {
    this.subscription = this.prodService.update(name)
    .subscribe(
      data => {
       this.loading = false;
       console.log(data); 
     },
     error => {
       this.loading = false;
       console.log(error);
     });
  }
}
4

2 回答 2

1

试试下面的:

onUpdate(form: NgForm) {
  this.loading = true;

  name =  form.value.name,
var _confirm = confirm('Are you sure you want to update?');
  if (_confirm ) {
    this.subscription = this.prodService.update(name)
    .subscribe(
      data => {
       this.loading = false;
       console.log(data); 
     },
     error => {
       this.loading = false;
       console.log(error);
     });
  }
else {
 this.loading = false;
}
}
于 2017-11-28T11:41:36.247 回答
1

当用户单击取消时,只需添加一个 else 条件。

onUpdate(form: NgForm) {
  this.loading = true;

  name =  form.value.name,

  if (confirm('Are you sure you want to update?')) {
    this.subscription = this.prodService.update(name)
    .subscribe(
      data => {
       this.loading = false;
       console.log(data); 
     },
     error => {
       this.loading = false;
       console.log(error);
     });
  } else {
    this.loading = false;
  }
}
于 2017-11-28T11:41:46.683 回答