Here is my component in Angular 4:
@Component( {
selector: \'input-extra-field\',
template: `
I was facing this issue while running unit tests. To fix I added the MatSlideToggleModule to the imports in my spect.ts file.
I fixed this error by adding the name="fieldName" ngDefaultControl
attributes to the element that carries the [(ngModel)]
attribute.
I also had the same error , angular 7
<button (click)="Addcity(city.name)" [(ngModel)]="city.name" class="dropdown-item fontstyle"
*ngFor="let city of Cities; let i = index">
{{city.name}}
</button>
I just added ngDefaultControl
<button (click)="Addcity(city.name)" [(ngModel)]="city.name" ngDefaultControl class="dropdown-item fontstyle"
*ngFor="let city of Cities; let i = index">
{{city.name}}
I also received this error while writing a custom form control component in Angular 7. However, none of the answers are applicable to Angular 7.
In my case, the following needed to be add to the @Component
decorator:
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => MyCustomComponent), // replace name as appropriate
multi: true
}
]
This is a case of "I don't know why it works, but it does." Chalk it up to poor design/implementation on the part of Angular.
Have you tried moving your [(ngModel)]
to the div
instead of the switch
in your HTML? I had the same error appear in my code and it was because I bound the model to a <mat-option>
instead of a <mat-select>
. Though I am not using form control.
In my case it was a component.member which was not existing e.g.
[formControl]="personId"
Adding it to the class declaration fixed it
this.personId = new FormControl(...)