gpt4 book ai didi

html - Angular 2 表单验证错误 "Unhandled Promise rejection: Cannot assign to a reference or variable!"

转载 作者:技术小花猫 更新时间:2023-10-29 12:47:48 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com