对formGroup禁用ng-invalid

时间:2018-06-25 14:25:10

标签: css angular

我有一个CSS属性:

.ng-dirty.ng-invalid:not(form):not(formGroup) {
  border-left: 5px solid #ff5663; /* red */
}

当表单输入无效时,这会给我红色指示。

但是我有一种情况,我的表单是formArray

<form (ngSubmit)="onSubmit()" [formGroup]="competitorForm">
  <div formArrayName="competitors">
    <div class="row" *ngFor="let competitor of competitorForm.controls['competitors'].controls; let i = index"
           formGroupName="{{i}}">
        <div class="col-md-3">
          <div class=" form-group">
            <input type="text" class="input-lg" formControlName="firstname" placeholder="First name"
                   [ngClass]="{ 'is-invalid': submitted && competitor.controls.firstname.errors }">
            <div *ngIf="(submitted && competitor.controls.firstname.errors)" class="text-primary">
              <div *ngIf="competitor.controls.firstname.errors.required" translate
                   [translateParams]="{attribute: 'firstname'}">
                validation.required
              </div>
            </div>
          </div>
        </div>
    </div>
  </div>
</form>

我有2个红色左边框,一个是字段,一个是行

我尝试过:

.ng-dirty.ng-invalid:not(form):not(formGroup) {
  border-left: 5px solid #ff5663; /* red */
}

但是它似乎不起作用。知道如何禁用行的红色边框吗?

1 个答案:

答案 0 :(得分:1)

如果您不想在带有类行的div上显示红色边框,可以尝试将:not(.row)添加到选择器中。

.ng-dirty.ng-invalid:not(form):not(formGroup):not(.row) {
  border-left: 5px solid #ff5663; /* red */
}

.not CSS选择器文档:https://developer.mozilla.org/en-US/docs/Web/CSS/:not

看起来您还需要对formGroup案例应用:not(.classname)语法,如下所示:

.ng-dirty.ng-invalid:not(form):not(.formGroup):not(.row) {
  border-left: 5px solid #ff5663; /* red */
}