gpt4 book ai didi

Angular 模板验证表单

转载 作者:太空狗 更新时间:2023-10-29 18:19:29 24 4
gpt4 key购买 nike

我正在处理表单验证。我正在使用模板驱动的验证表单,其编码如下。它现在工作正常,但是当我尝试在输入中添加 #username = "ngModel"和 #password = "ngModel"以创建验证类时,它向我显示错误。也请找出错误。

<div class="container">
<div class="row">
<div class="centering text-center">
<div class="login-cont col-md-4 col-md-offset-4 vcenter">
<form id="login_form" name="login-form" #f="ngForm" role="form" (ngSubmit)="f.form.valid && login()" novalidate>

<input id="username" [(ngModel)]="username" name="username" required class="form-control" type="text" placeholder="Username" >
<input id="userPassword" class="form-control" required type="password" name="userPassword" required placeholder="Password" [(ngModel)]="password" >
<button type="submit" class="btn login-btn">Login</button>
</form>
</div>
</div>
</div>
</div>

 Cannot assign to a reference or variable!
at _AstToIrVisitor.visitPropertyWrite (webpack-internal:///../../../compiler/esm5/compiler.js:26226:23)
at PropertyWrite.visit (webpack-internal:///../../../compiler/esm5/compiler.js:4803:24)
at convertActionBinding (webpack-internal:///../../../compiler/esm5/compiler.js:25676:45)
at eval (webpack-internal:///../../../compiler/esm5/compiler.js:28166:22)
at Array.forEach (<anonymous>)
at ViewBuilder._createElementHandleEventFn (webpack-internal:///../../../compiler/esm5/compiler.js:28162:18)
at nodes.(anonymous function) (webpack-internal:///../../../compiler/esm5/compiler.js:27581:27)
at eval (webpack-internal:///../../../compiler/esm5/compiler.js:28107:22)
at Array.map (<anonymous>)
at ViewBuilder._createNodeExpressions (webpack-internal:///../../../compiler/esm5/compiler.js:28106:56)

这是我尝试过的,但它给了我一个错误

   <input id="username" class="form-control"  type="text" required  name="username" placeholder="Username" [(ngModel)]="username" minlength="4"  #names="ngModel">


<div *ngIf="names.invalid && (names.dirty || names.touched)"
class="alert alert-danger">
</div>
<div *ngIf="names.errors.required">
Name is required.
</div>

<div *ngIf="names.errors.minlength">
Name must be at least 4 characters long.
</div>

最佳答案

You are getting error because of your variable name and local variable name both are same

username 是您分配给 [(ngModel)]='username' 的变量,同时您还试图使局部变量 #用户名

使用不同的名称,如 #username2#password2 ,将解决您的问题。

更多详情请阅读:

https://scotch.io/tutorials/using-angular-2s-template-driven-forms

关于Angular 模板验证表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47300135/

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