gpt4 book ai didi

angular - 如果类在模板驱动形式 Angular 4 中无效,为什么类不应用于输入元素?

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

我为我的登录页面使用了模板驱动的表单。如果输入元素无效或有任何错误,我希望在输入元素上有红色边框,但当输入为空白或无效时,类 borderRed 不会添加到输入元素上。

<form (ngSubmit)="f.form.valid && signin()" #f="ngForm" class="m-login__form m-form" action="">
<ng-template #alertSignin></ng-template>

<div class="form-group form-md-line-input form-md-floating-label">
<label>
Email <span class="text-danger">*</span>
</label>
<input [class.borderRed]="email.invalid" class="form-control m-input" type="text" required="required" name="email" [(ngModel)]="model.email" #email="ngModel" autocomplete="off">
</div>

<div class="form-group form-md-line-input form-md-floating-label">
<label [pTooltip]="attachementTooltipmsg" tooltipPosition="top" for="form_control_1">
Password <span class="text-danger">*</span>
</label>
<input [class.borderRed]="password.invalid" class="form-control m-input" required="" name="password" type="password" [(ngModel)]="model.password" #password="ngModel">
</div>

<div class="m-login__form-action">
<button [disabled]="loading" [ngClass]="{'m-loader m-loader--right m-loader--light': loading}" id="m_login_signin_submit" class="btn btn-focus m-btn m-btn--pill m-btn--custom m-btn--air">
Sign In
</button>
</div>
</form>

最佳答案

使用这个

   <input [class.borderRed]="email.touched && !email.valid" class="form-control m-input" type="email" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3}$" required name="email" [(ngModel)]="model.email" #email="ngModel" autocomplete="off">

查看 StackBiltz

Edited code

关于angular - 如果类在模板驱动形式 Angular 4 中无效,为什么类不应用于输入元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50062072/

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