import {AfterViewInit, Component, ElementRef} from '@angular/core'; import {FormControl, FormGroup, Validators} from '@angular/forms'; import {IOption} from 'ng-select'; declare var hljs: any; import {OptionService} from '../../services/option.service'; @Component({ selector: 'form-validation', templateUrl: './form-validation.component.html' }) export class FormValidation implements AfterViewInit { characters: Array = this.optionService.getCharacters(); form0: FormGroup; form1: FormGroup; constructor( private elementRef: ElementRef, private optionService: OptionService ) {} ngOnInit() { this.form0 = new FormGroup({ character: new FormControl('', Validators.required) }); this.form1 = new FormGroup({ character: new FormControl([], Validators.required) }); } ngAfterViewInit() { hljs.initHighlighting(); let nodes: NodeList = this.elementRef .nativeElement .querySelectorAll('.typescript, .html, .css'); for (let i = 0; i < nodes.length; i++) { hljs.highlightBlock(nodes[i]); } } html0: string = `
Form valid: {{form.valid}}
<form
    novalidate
    [formGroup]="form">
    <ng-select
        formControlName="character"
        [allowClear]="true"
        [options]="characters">
    </ng-select>
    <button
        [disabled]="!form.valid">
        Submit
    </button>
</form>
`; ts0: string = `
export class FormValidationExample {

    characters: Array<IOption> = this.optionService.getOptions();

    constructor(
        private optionService: OptionService
    ) {}

    ngOnInit() {
        this.form = new FormGroup({
            character: new FormControl('', Validators.required)
        });
    }
}
`; html1: string = `
Form valid: {{form.valid}}
<form
    novalidate
    [formGroup]="form">
    <ng-select
        formControlName="character"
        [multiple]="true"
        [options]="characters">
    </ng-select>
    <button
        [disabled]="!form.valid">
        Submit
    </button>
</form>
`; ts1: string = `
export class FormValidationExample {

    characters: Array<IOption> = this.optionService.getOptions();

    constructor(
        private optionService: OptionService
    ) {}

    ngOnInit() {
        this.form = new FormGroup({
            character: new FormControl([], Validators.required)
        });
    }
}
`; }