我需要让输入框的颜色根据其状态(有效或无效)更改颜色。我正在关注一个教程,在他的教程中它可以更改。我复制并粘贴了他的代码,因此我可以确定自己没有丢失任何东西,但是我仍然无法使它正常工作。请在下面查看我的代码:
styles.css
input.ng-touched.ng-invalid{
border-color: #dc3545;
}
input.ng-valid{
border-color: #28a745;
}
payment-detail-component.ts
<form #form="ngForm" autocomplete="off">
<input type="hidden" name="PMId" [value]="service.formData.PMId">
<div class="form-group">
<div class="input-group">
<div class="input-group-prepend">
<div class="input-group-text bg-white">
<i class="fas fa-user-circle" [class.green-icon]="CardOwnerName.valid" [class.red-icon]="CardOwnerName.invalid && CardOwnerName.touched"></i>
</div>
</div>
<input name="CardOwnerName" #CardOwnerName="ngModel" [(ngModel)]="service.formData.CardOwnerName" class="form-control"
placeholder="Card Owner Name" required>
</div>
</div>
<div class="form-group">
<div class="input-group">
<div class="input-group-prepend">
<div class="input-group-text bg-white">
<i class="far fa-credit-card" [class.green-icon]="CardNumber.valid" [class.red-icon]="CardNumber.invalid && CardNumber.touched"></i>
</div>
</div>
<input name="CardNumber" #CardNumber="ngModel" [(ngModel)]="service.formData.CardNumber"
class="form-control" placeholder="16 Digit Card Number" required maxlength="16" minlength="16">
</div>
</div>
<div class="form-row">
<div class="form-group col-md-7">
<div class="input-group">
<div class="input-group-prepend">
<div class="input-group-text bg-white">
<i class="fas fa-calendar-alt" [class.green-icon]="ExpirationDate.valid" [class.red-icon]="ExpirationDate.invalid && ExpirationDate.touched"></i>
</div>
</div>
<input name="ExpirationDate" #ExpirationDate="ngModel" [(ngModel)]="service.formData.ExpirationDate" class="form-control"
placeholder="MM/YY" required maxlength="5" minlength="5">
</div>
</div>
<div class="form-group col-md-5">
<div class="input-group">
<div class="input-group-prepend">
<div class="input-group-text bg-white">
<i class="fas fa-key" [class.green-icon]="CVV.valid" [class.red-icon]="CVV.invalid && CVV.touched"></i>
</div>
</div>
<input type="password" name="CVV" #CVV="ngModel" [(ngModel)]="service.formData.CVV" class="form-control" placeholder="CVV"
required maxlength="3" minlength="3">
</div>
</div>
</div>
<div class="form-group">
<button class="btn btn-success btn-lg btn-block" type="submit" [disabled]="form.invalid"><i class="fas fa-database"></i> Submit</button>
</div>
</form>
使用检查器时,我应该看到iput框的不同状态,例如“原始”,“触摸”,“脏污”等。但是我没有看到它。 你能告诉我如何做到这一点吗?谢谢。
答案 0 :(得分:1)
对于那些感兴趣的人,代码是正确的。我的问题是ngModels未定义,因此我需要在component.ts文件中执行此操作:
formData: PaymentDetail = {
CVV: null,
CardNumber: null,
CardOwnerName: null,
ExpirationDate: null,
PMId: null
}
答案 1 :(得分:0)
问题出在payment-details.service.ts
像这样添加formData:
export class PaymentDetailService {
formData: PaymentDetail = {
CVV: null,
CardNumber: null,
CardOwnerName: null,
ExpirationDate: null,
PMId: null
}