I have a form which has an input. I applied two-way event binding on the input, however, it doesn't work. I have imported the FormsModule in my AppModule and I am not getting any errors.
search.component.html:
<form>
<h1>{{ query }}</h1>
<input [(ngModel)]="query">
</form>
search.component.ts:
import { Component } from '@angular/core';
@Component({
selector: 'app-search',
templateUrl: './search.component.html',
styleUrls: ['./search.component.scss']
})
export class SearchComponent {
public query: string;
constructor() {}
}
It works when I do not include the form tag:
<h1>{{ query }}</h1>
<input [(ngModel)]="query">
But I need to use form for submission. Any ideas what's wrong?
Relevant thread.
I got the following error later, not sure why it didn't show up earlier:
As suggested, I resolved it by adding
[ngModelOptions]="{standalone: true}":You could also choose the second route as specified, i.e., add a
nameattribute and put anything: