Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
88
rated 0 times [  94] [ 6]  / answers: 1 / hits: 21651  / 6 Years ago, sun, june 10, 2018, 12:00:00

I have tried importing the FormsModule and NgForm modules as well as adding the FormsModule to the imports array.



Below is my code:



//our root app component
import { Component, NgModule, VERSION } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import {FormsModule, NgForm} from '@angular/forms';

@Component({
selector: 'my-app',
template: `
<form #searchForm=ngForm>
<input type=text required [(ngModel)]=model.search ngControl=search #inputSearch=ngForm>
<p class=error [hidden]=inputSearch.valid> This input is required</p>


</form>

`,
styles: [`
.error {
color: red;
font-size: 11px;
}
`]
})
export class App {
public model = {
search:
}

constructor() {

}
}

@NgModule({
imports: [BrowserModule, FormsModule],
declarations: [App],
bootstrap: [App],
})
export class AppModule {}


And below is an error printout:




runtime.9ff156e16d788666a54a.js:16 Error: Template parse errors:
There is no directive with exportAs set to ngForm ( ]#searchForm=ngForm> ]#inputSearch=ngForm> This input is required

):
ng:///AppModule/App.html@2:76 Can't bind to 'ngModel' since it isn't a
known property of 'input'. ( ][(ngModel)]=model.search
ngControl=search #inputSearch=ngForm> https://run.plnkr.co/rhpwnL6UIQwCFOKZ/src/main.js Loading
https://run.plnkr.co/rhpwnL6UIQwCFOKZ/src/main.js f @
runtime.9ff156e16d788666a54a.js:16



More From » angular

 Answers
11

The error was caused by this line:



#inputSearch=ngForm


This is the correct line:



#inputSearch=ngModel


Here is the working example. When you use ngModel within the form tag you also need to provide value for the name attribute.



  <form #searchForm=ngForm>
<input type=text required name=search [(ngModel)]=model.search #inputSearch=ngModel>
<p class=error [hidden]=inputSearch.valid> This input is required</p>
</form>

[#54231] Wednesday, June 6, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
robinh

Total Points: 371
Total Questions: 105
Total Answers: 89

Location: Cyprus
Member since Mon, Oct 24, 2022
2 Years ago
robinh questions
Thu, May 6, 21, 00:00, 3 Years ago
Thu, Jan 7, 21, 00:00, 3 Years ago
Fri, Nov 6, 20, 00:00, 4 Years ago
Sun, Aug 9, 20, 00:00, 4 Years ago
;