Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 在SweetAlert2模态角度2(4)的结果中调用服务或变量_Angular_Sweetalert_Sweetalert2 - Fatal编程技术网

Angular 在SweetAlert2模态角度2(4)的结果中调用服务或变量

Angular 在SweetAlert2模态角度2(4)的结果中调用服务或变量,angular,sweetalert,sweetalert2,Angular,Sweetalert,Sweetalert2,我使用角度和角度。我的sweetalert2工作得非常好,直到我想在确认后调用服务或任何函数或变量 我的组成部分: import swal from 'sweetalert2'; @Component({ selector: 'app-filter', templateUrl: './filter.component.html', }) export class FilterComponent implements OnInit { private swal: any; c

我使用角度和角度。我的sweetalert2工作得非常好,直到我想在确认后调用服务或任何函数或变量

我的组成部分:

import swal from 'sweetalert2';

@Component({
  selector: 'app-filter',
  templateUrl: './filter.component.html',
})
export class FilterComponent implements OnInit {

  private swal: any;

  constructor(private asyncService: AsyncService) {

  }

  ngOnInit() {
      this.getCurrentData(1);
  }

  getCurrentData(id: number) {
    this.asyncService.getCurrentFilterData(id)
      .subscribe(
        response => {
          this.filter = response.json();
        },
        error => console.log('error : ' + error)
      );
  }

  saveFilter() {
    swal({
      title: 'Are you sure?',
      text: 'You wish to save filter changes!',
      type: 'question',
      showCancelButton: true,
      confirmButtonText: 'Save',
      cancelButtonText: 'Cancel'
    }).then(function() {

      this.asyncService.filterUpdate(this.filter) // can't access to this. (this.filter or this.asyncService)
        .subscribe(
          response => {
              console.log('success');
          },
          error => console.log('error : ' + error)
        );
    });
  }

有什么解决方案吗?我如何访问它?

如果希望
将绑定到组件实例,请使用箭头函数

.then(() => {
而不是

.then(function() {

定义一个类字段
let self并初始化
self=this
ngOnInit()
中。现在进入
。然后()
您可以使用
self
而不是
this