情况:
请帮忙!我试图在我的Angular2应用程序中制作一个非常简单的表单,但无论它从不工作.
角度版本:
Angular 2.0.0 Rc5
错误:
Can't bind to 'formGroup' since it isn't a known property of 'form'
Run Code Online (Sandbox Code Playgroud)
代码:
风景:
<form [formGroup]="newTaskForm" (submit)="createNewTask()">
<div class="form-group">
<label for="name">Name</label>
<input type="text" name="name" required>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
Run Code Online (Sandbox Code Playgroud)
控制器:
import { Component } from '@angular/core';
import { FormGroup, FormControl, Validators, FormBuilder } from '@angular/forms';
import {FormsModule,ReactiveFormsModule} from '@angular/forms';
import { Task } from './task';
@Component({
selector: 'task-add',
templateUrl: 'app/task-add.component.html'
})
export class TaskAddComponent {
newTaskForm: FormGroup;
constructor(fb: FormBuilder)
{
this.newTaskForm …Run Code Online (Sandbox Code Playgroud) 我使用下面的代码在angularjs 2.0中提交表单,但是得到错误,没有导出的指令设置为ngForm和ngModel,但我正在使用.以下是我的表格:
<form name="form" (ngSubmit)="f.form.valid && register()" #f="ngForm" novalidate>
<div class="form-group" [ngClass]="{ 'has-error': f.submitted && !username.valid }">
<label for="firstName">First Name</label>
<input type="text" class="form-control" name="firstName" [(ngModel)]="model.firstName" #firstName="ngModel"
required/>
<div *ngIf="f.submitted && !firstName.valid" class="help-block">First Name is required</div>
</div>
<div class="form-group" [ngClass]="{ 'has-error': f.submitted && !username.valid }">
<label for="lastName">Last Name</label>
<input type="text" class="form-control" name="lastName" [(ngModel)]="model.lastName" #lastName="ngModel" required/>
<div *ngIf="f.submitted && !lastName.valid" class="help-block">Last Name is required</div>
</div>
<div class="form-group" [ngClass]="{ 'has-error': f.submitted && !username.valid }">
<label for="username">Username</label>
<input type="text" class="form-control" name="username" [(ngModel)]="model.username" #username="ngModel" required/> …Run Code Online (Sandbox Code Playgroud) 我正在以角度创建表单验证,但出现错误
未找到带有 exportAs 'ngModel' 的指令。
我的代码:
<form>
<div class="form-group">
<label for="firstname">First Name</label>
<input type="text" id="name" name="name" required minlength="4"
appForbiddenName="bob" ngModel #name="ngModel">
<div class="alert alert-danger" *ngIf>First name required</div>
</div>
<div class="form-group">
<label for="comment">Comment</label>
<textarea name="" id="comment" cols="30" rows="10" class="form-control"></textarea>
</div>
<button class="btn btn-primary">Submit</button>
</form>
Run Code Online (Sandbox Code Playgroud)

错误:
Error: src/app/app.component.html:6:60 - error NG8003: No directive found with exportAs 'ngModel'.
6 appForbiddenName="bob" ngModel #name="ngModel">
~~~~~~~
src/app/app.component.ts:5:16
5 templateUrl: './app.component.html',
~~~~~~~~~~~~~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)
组件AppComponent的模板出现错误。