i have following code
removeAlert() : Observable<boolean> {
Swal.fire({
title: 'delete this file ?',
text: 'sth',
icon: 'warning',
showCancelButton: true,
confirmButtonText: 'ok',
cancelButtonText: 'cancel'
}).then((result) => {
if (result.value) {
return of(true) ;
}
else if (result.dismiss === Swal.DismissReason.cancel) {
this.showError("canceled" , "sth else");
return of(false) ;
}
});
//return of(false) ;
}
how i can observe value after click and return it
i mean this part if(result.value)
Create a Subject and emit a next
value on it within your if/else
statement. Be sure to .subscribe()
to the Subject prior to emitting a value otherwise nothing will happen. In my code I've subscribed in the ngOnInit
life cycle method:
myBool: boolean;
mySubject: Subject<boolean> = new Subject();
ngOnInit() {
this.mySubject.subscribe(value => console.log('Value: ', value));
}
removeAlert() {
if (this.myBool) {
this.mySubject.next(true);
} else {
this.mySubject.next(false);
}
this.myBool = !this.myBool;
}
Bind the click event to the removeAlert()
method in the component template:
<button (click)="removeAlert()">Click me</button>
Here is a stackblitz demo with working code of the above.