Angular 在SweetAlert2模态角度2(4)的结果中调用服务或变量
我使用角度和角度。我的sweetalert2工作得非常好,直到我想在确认后调用服务或任何函数或变量 我的组成部分: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
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