未捕获的错误:模板解析错误:在angular8中

时间:2019-11-14 17:31:59

标签: angular typescript

我按照this教程进行了自己的项目。

当我运行我的角度应用程序时,控制台中会出现以下错误。

Uncaught Error: Template parse errors:
Can't bind to 'ngModel' since it isn't a known property of 'input'. ("for="name">First Name</label>
      <input type="text" class="form-control" id="firstName" required [ERROR ->][(ngModel)]="employee.firstName" name="firstName">
    </div>

"): ng:///AppModule/CreateEmployeeComponent.html@5:70
Can't bind to 'ngModel' since it isn't a known property of 'input'. ("l for="name">Last Name</label>
      <input type="text" class="form-control" id="lastName" required [ERROR ->][(ngModel)]="employee.lastName" name="lastName">
    </div>

"): ng:///AppModule/CreateEmployeeComponent.html@10:69
Can't bind to 'ngModel' since it isn't a known property of 'input'. ("l for="name">First Name</label>
      <input type="text" class="form-control" id="emailId" required [ERROR ->][(ngModel)]="employee.emailId" name="emailId">
    </div>

"): ng:///AppModule/CreateEmployeeComponent.html@15:68
    at syntaxError (compiler.js:2175)
    at TemplateParser.parse (compiler.js:11388)
    at JitCompiler._parseTemplate (compiler.js:25963)
    at JitCompiler._compileTemplate (compiler.js:25951)
    at compiler.js:25895
    at Set.forEach (<anonymous>)
    at JitCompiler._compileComponents (compiler.js:25895)
    at compiler.js:25808
    at Object.then (compiler.js:2166)
    at JitCompiler._compileModuleAndComponents (compiler.js:25807)

由于我仔细地遵循了上面的教程,所以我无法弄清楚这段代码有什么问题。

这是我的html文件。

<h3>Create Employee</h3>
<div [hidden]="submitted" style="width: 400px;">
  <form (ngSubmit)="onSubmit()">
    <div class="form-group">
      <label for="name">First Name</label>
      <input type="text" class="form-control" id="firstName" required [(ngModel)]="employee.firstName" name="firstName">
    </div>

    <div class="form-group">
      <label for="name">Last Name</label>
      <input type="text" class="form-control" id="lastName" required [(ngModel)]="employee.lastName" name="lastName">
    </div>

    <div class="form-group">
      <label for="name">First Name</label>
      <input type="text" class="form-control" id="emailId" required [(ngModel)]="employee.emailId" name="emailId">
    </div>

    <button type="submit" class="btn btn-success">Submit</button>
  </form>
</div>

<div [hidden]="!submitted">
  <h4>You submitted successfully!</h4>
  <!-- <button class="btn btn-success" (click)="newEmployee()">Add</button> -->
</div>

1 个答案:

答案 0 :(得分:1)

从'@ angular / forms'导入{FormsModule}; 从'@ angular / common / http'导入{HttpClientModule};

进口:[ BrowserModule, AppRoutingModule, 表格模块 HttpClientModule ]

在您的app.module.ts文件中添加这两个导入