作者热门文章
- objective-c - iOS 5 : Can you override UIAppearance customisations in specific classes?
- iphone - 如何将 CGFontRef 转换为 UIFont?
- ios - 以编程方式关闭标记的信息窗口 google maps iOS
- ios - Xcode 5 - 尝试验证存档时出现 "No application records were found"
App.component.html
<div class="container">
<h2>Form Validation</h2>
<form>
<div class="form-group">
<label for="prettyName">Name</label>
<input type="text" class="form-control" id="prettyName" required minlength="4" maxlength="20" [(ngModel)]="prettyName" name="prettyName" #name="ngModel">
<div *ngIf="name.errors && (name.dirty || name.touched)" class="alert alert-danger">
<div [hidden]="!name.errors.required">
Name is required
</div>
<div [hidden]="!name.errors.minlength">
Name must be at least 4 characters long
</div>
<div [hidden]="!name.errors.maxlength">
Name cannot be more than 20 characters long
</div>
</div>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
// ... (Same things for username, email and password)
App.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
prettyName: string;
username: string;
email: string;
password: string;
}
我遵循了关于表单验证的官方文档:https://angular.io/docs/ts/latest/cookbook/form-validation.html#!#template1
请问有人知道这个错误是从哪里来的吗?
干杯
最佳答案
您应该更改组件变量或模板 #name
变量。他们正在碰撞:
export class AppComponent {
prettyname: string; // here
username: string;
email: string;
password: string;
}
同时将 String
更改为 string
<form>
<div class="form-group">
<label for="prettyName">Name</label>
<input type="text" class="form-control" id="prettyName" required minlength="4" maxlength="20" [(ngModel)]="prettyName" name="prettyName" #name="ngModel">
<div *ngIf="name.errors && (name.dirty || name.touched)" class="alert alert-danger">
<div [hidden]="!name.errors.required">
Name is required
</div>
<div [hidden]="!name.errors.minlength">
Name must be at least 4 characters long
</div>
<div [hidden]="!name.errors.maxlength">
Name cannot be more than 20 characters long
</div>
</div>
</div>
<button type="submit" class="btn btn-default">Submit</button>
关于html - Angular 2 表单验证错误 "Unhandled Promise rejection: Cannot assign to a reference or variable!",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42437822/
我是一名优秀的程序员,十分优秀!