Angualr2.0报错TypeError: Cannot read property 'form' of undefined

09-29 17:09:23   来源:shikezhi   评论: 点击:

core umd js:3462 EXCEPTION: Error in template hero-form component html:25:54 caused by: Cannot read property form of undefinedErrorHandler handleError @ core umd js:3462(anonymous function) @ core umd js:6860ZoneDelegate invoke @ zone js:203onInvoke
 
core.umd.js:3462 EXCEPTION: Error in template/hero-form.component.html:25:54 caused by: Cannot read property 'form' of undefinedErrorHandler.handleError @ core.umd.js:3462(anonymous function) @ core.umd.js:6860ZoneDelegate.invoke @ zone.js:203onInvoke @ core.umd.js:6242ZoneDelegate.invoke @ zone.js:202Zone.run @ zone.js:96(anonymous function) @ zone.js:462ZoneDelegate.invokeTask @ zone.js:236onInvokeTask @ core.umd.js:6233ZoneDelegate.invokeTask @ zone.js:235Zone.runTask @ zone.js:136drainMicroTaskQueue @ zone.js:368ZoneTask.invoke @ zone.js:308
core.umd.js:3464 ORIGINAL EXCEPTION: Cannot read property 'form' of undefinedErrorHandler.handleError @ core.umd.js:3464(anonymous function) @ core.umd.js:6860ZoneDelegate.invoke @ zone.js:203onInvoke @ core.umd.js:6242ZoneDelegate.invoke @ zone.js:202Zone.run @ zone.js:96(anonymous function) @ zone.js:462ZoneDelegate.invokeTask @ zone.js:236onInvokeTask @ core.umd.js:6233ZoneDelegate.invokeTask @ zone.js:235Zone.runTask @ zone.js:136drainMicroTaskQueue @ zone.js:368ZoneTask.invoke @ zone.js:308
core.umd.js:3467 ORIGINAL STACKTRACE:ErrorHandler.handleError @ core.umd.js:3467(anonymous function) @ core.umd.js:6860ZoneDelegate.invoke @ zone.js:203onInvoke @ core.umd.js:6242ZoneDelegate.invoke @ zone.js:202Zone.run @ zone.js:96(anonymous function) @ zone.js:462ZoneDelegate.invokeTask @ zone.js:236onInvokeTask @ core.umd.js:6233ZoneDelegate.invokeTask @ zone.js:235Zone.runTask @ zone.js:136drainMicroTaskQueue @ zone.js:368ZoneTask.invoke @ zone.js:308
core.umd.js:3468 TypeError: Cannot read property 'form' of undefined
    at DebugAppView._View_HeroFormComponent0.detectChangesInternal (HeroFormComponent.ngfactory.js:810)
    at DebugAppView.AppView.detectChanges (core.umd.js:9566)
    at DebugAppView.detectChanges (core.umd.js:9671)
    at DebugAppView.AppView.detectViewChildrenChanges (core.umd.js:9592)
    at DebugAppView._View_AppComponent0.detectChangesInternal (AppComponent.ngfactory.js:1357)
    at DebugAppView.AppView.detectChanges (core.umd.js:9566)
    at DebugAppView.detectChanges (core.umd.js:9671)
    at DebugAppView.AppView.detectViewChildrenChanges (core.umd.js:9592)
    at DebugAppView.AppView.detectChangesInternal (core.umd.js:9577)
    at DebugAppView.AppView.detectChanges (core.umd.js:9566)
 
 
 
解决办法:将form表单元素添加模板变量并将其赋值给NgForm,如:
 
<form (ngSubmit)="onSubmit(theForm)" #theForm="ngForm">
  <div class="form-group">
    <label for="name">Name</label>
    <input class="form-control" name="name" required [(ngModel)]="currentHero.firstName">
  </div>
  <button type="submit" [disabled]="!theForm.form.valid">Submit</button>
</form>    
 
 

相关热词搜索:Angualr2 0 TypeError

上一篇:Angular2.0报错If ngModel is used within a form tag, either the name attribute 下一篇:浅谈Angular中ngModel的$render
分享到: 收藏