Question
Why is my boolean status not resetting after Subscribe method?
I have a simple Submit button and have Mat-Spinner to show the progress. After the call either its Success or Error, my isLoading is not setting to False, because of which my spinner is showing always. I think i am missing something in Angular 18, the Standalone component. Please help
Here is the below code
import { CommonModule } from '@angular/common';
import { ChangeDetectionStrategy, Component } from '@angular/core';
import {
FormBuilder,
FormGroup,
FormGroupDirective,
FormsModule,
ReactiveFormsModule,
Validators,
} from '@angular/forms';
import { Router } from '@angular/router';
import { MaterialModule } from '../../material-module';
import { ApiService } from 'src/app/services/api.service';
import { HttpErrorResponse } from '@angular/common/http';
@Component({
providers: [FormGroupDirective],
selector: 'app-basic',
standalone: true,
imports: [
CommonModule,
MaterialModule,
FormsModule,
ReactiveFormsModule,
],
templateUrl: './basic.component.html',
styleUrl: './basic.component.css',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class BasicComponent {
basicInfo: FormGroup;
isLoading: boolean;
errorMsg: string;
constructor(
private fb: FormBuilder,
public router: Router,
private apiSvc: ApiService,
) {}
Submit Button Code
submit() {
if (this.basicInfo.valid) {
this.isLoading = true;
this.apiSvc.create(this.basicInfo.value).subscribe({
next: (data) => {
this.isLoading = false;
},
error: (error: HttpErrorResponse) => {
this.isLoading = false;
},
});
}
}
<button
mat-flat-button
[disabled]="!basicInfo.valid"
class="custom-width-button"
>
<mat-spinner
[diameter]="24"
class="white-spinner"
*ngIf="isLoading"
></mat-spinner>
</button>