getDays() { this.workingDays = []; this.storeLocatorServ.getDays().subscribe((response:…
- User
getDays() { this.workingDays = []; this.storeLocatorServ.getDays().subscribe((response: any) => { response.data.forEach(d => { this.workingDays.push({ id: d.id, dayEn: d.dayEn, hrFrom: "", hrTo: "", closed: false, fullDay: false }) }); }) } i have this function and i got this console error store-locator.component.ts:200 ERROR Error: NG01352: If ngModel is used within a form tag, either the name attribute must be set or the form control must be defined as 'standalone' in ngModelOptions. at this.storeLocatorServ.getDays().subscribe((response: any) => {
- Assistant
This error occurs because ngModel is being used within a form tag without the name attribute set. To resolve this error, you can either set the name attribute for the input elements using ngModel or define the form control as 'standalone' in ngModelOptions.
Here's an example of how you can set the name attribute for the input elements using ngModel:
<form> <div *ngFor="let day of workingDays"> <input [(ngModel)]="day.hrFrom" name="hrFrom_{{day.id}}"> <input [(ngModel)]="day.hrTo" name="hrTo_{{day.id}}"> <input type="checkbox" [(ngModel)]="day.closed" name="closed_{{day.id}}"> <input type="checkbox" [(ngModel)]="day.fullDay" name="fullDay_{{day.id}}"> </div> </form>Alternatively, you can also define the form control as 'standalone' in ngModelOptions:
import { NgModel } from '@angular/forms'; constructor(private ngModel: NgModel) { ngModel.options = { standalone: true }; }By doing one of the above solutions, the error should be resolved.