没有内置required attribute
的ng-select
。我总是通过form
验证来验证它。
在 angular2 或更高版本中,您可以将ng-select
withngNativeValidate
指令用于模板驱动的表单。
<form #registerUser="ngForm" ngNativeValidate (ngSubmit)="onSubmit()">
<ng-select
class="col-md-8 required"
[items]="options"
bindLabel="label"
bindValue="value"
[multiple]="true"
name="skills"
placeholder="Select Skills"
[(ngModel)]="registerUserData.skills"
required>
</ng-select>
<div *ngIf="!registerUserData.skills" class="invalid-feedback">
Skills are required
</div>
<button type='submit' [disabled]="!registerUser.valid" [isFormValid]="!registerUser.valid">Submit</button>
</form>
当或和时,我也使用自定义style
红色标记字段。您可以将其放在全局或文件中以与 all 一样工作。untouched
touched
invalid
scss
css
component
SCSS:
ng-select.required.ng-invalid.ng-touched {
.ng-select-container{
border-color: #dc3545;
box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 0 3px #fde6e8;
}
}
ng-select.required.ng-invalid.ng-untouched {
.ng-select-container{
border-color: #dc3545;
box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 0 3px #fde6e8;
}
}
CSS:
ng-select.required.ng-invalid.ng-touched .ng-select-container{
border-color: #dc3545;
box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 0 3px #fde6e8;
}
ng-select.required.ng-invalid.ng-untouched .ng-select-container {
border-color: #dc3545;
box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 0 3px #fde6e8;
}