Angular 2错误 - 没有指向“exportAs”的指令设置为“ngModel”与RC4版本 [英] Angular 2 error- There is no directive with "exportAs" set to "ngModel" with RC4 version
问题描述
我在我的应用程序中使用angular 2表单,并且我已根据给定的链接创建表单。
I am using angular 2 forms in my application and i have created the forms based on given link.
https://angular.io/docs/ts/latest/guide/forms.html
在此验证和使用表单API中,我设置了 ngModel
值,如#name =id #id =ngModel
并且会抛出脚本错误。但如果我将#id =ngModel
设置为 #id =ngForm
,则会解决。但对于我的情况,我必须将我的模型值设置为 ngModel
。
In this for validation and to use forms APIs, i have set the ngModel
values like #name="id" #id="ngModel"
and which throws script error. But its resolved if i set #id="ngModel"
as #id="ngForm"
. But for my case i have to set my model value to ngModel
.
以下是我的html页面。
Below is my html page.
<form (ngSubmit)="onSubmit()" #myForm="ngForm">
<div class="form-group">
<label class="control-label" for="id">Employee ID</label>
<input type="text" class="form-control" required [(ngModel)]="model.id" #name="id" #id="ngModel" >
<div [hidden]="id.valid || id.pristine" class="alert alert-danger">
Employee ID is required
</div>
</div>
<div class="form-group">
<label for="name">Employee Name</label>
<input type="text" class="form-control" [(ngModel)]="model.name" name="name" #name="ngModel" required>
<div [hidden]="name.valid || name.pristine" class="alert alert-danger">
Employee ID is required
</div>
</div>
<div class="form-group">
<label for="DOJ">DOJ</label>
<input class="form-control" required [(ngModel)]="model.DOJ" name="DOJ" #DOJ="ngModel" />
<div [hidden]="DOJ.valid || DOJ.pristine" class="alert alert-danger">
DOJ is required
</div>
</div>
<button type="submit" class="btn btn-default" [disabled]="!myForm.form.valid">Submit</button>
</form>
以下是我的问题。
EXCEPTION: Template parse errors:
There is no directive with "exportAs" set to "ngModel" ("
<div>
<h1>My Form</h1>
<form (ngSubmit)="onSubmit()" [ERROR ->]#myForm="ngModel">
<div class="form-group>
<label class="control-label" for="id">Employee"):AppComponent@3:34
我已经检查了更多问题和答案,他们中的大多数都说将angular2版本更新为 RC4
所以我已将我的应用程序更新为rc4但仍然我正面临这个问题。
I have checked with more questions and answers, most of them said to update angular2 version to RC4
so i have updated my application to rc4 but still i am facing this issue.
以下是我的ts档案:
import {Component} from '@angular/core';
import { disableDeprecatedForms, provideForms , NgForm} from '@angular/forms';
import {CORE_DIRECTIVES, FORM_DIRECTIVES, FormBuilder,Validators,Control,ControlGroup } from '@angular/common';
@Component({
selector: 'ej-app',
templateUrl: 'app/app.component.html',
directives: [ CORE_DIRECTIVES,FORM_DIRECTIVES]
})
export class AppComponent {
model = new Employees(null,'','');
onSubmit() { alert("values submitted")}
constructor() {
}
}
export class Employees {
constructor( public id: number,public name: string, public DOJ: String ) { }
}
推荐答案
不要混合使用新旧表单模块。
Don't mix the new and old forms module.
import {CORE_DIRECTIVES, FORM_DIRECTIVES, FormBuilder,Validators,Control,ControlGroup } from '@angular/common';
从 @ angular / common
导入表单内容。如果您使用新表格
imports forms stuff from @angular/common
. If you use the new forms
bootstrap(AppComponent, [disableDeprecatedForms(), provideForms()])
然后使用
import {FORM_DIRECTIVES, FormBuilder,Validators,Control,ControlGroup } from '@angular/forms';
这篇关于Angular 2错误 - 没有指向“exportAs”的指令设置为“ngModel”与RC4版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!