import {AfterViewInit, Component, ElementRef} from '@angular/core'; import {FormControl, FormGroup} from '@angular/forms'; import {IOption} from 'ng-select'; declare var hljs: any; import {OptionService} from '../../services/option.service'; @Component({ selector: 'reactive-form', templateUrl: 'reactive-form.component.html' }) export class ReactiveForm implements AfterViewInit { characters: Array = this.optionService.getCharacters(); defaultCharacter: string = '3'; defaultCharacters: Array = ['1', '3']; form0: FormGroup; form1: FormGroup; constructor( private elementRef: ElementRef, private optionService: OptionService ) {} ngOnInit() { this.form0 = new FormGroup({ character: new FormControl(this.defaultCharacter) }); this.form1 = new FormGroup({ character: new FormControl(this.defaultCharacters) }); } 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 = `
<div>
    <span>Form state: </span>
    <span *ngIf="form.get('character')?.dirty">dirty</span>
    <span *ngIf="form.get('character')?.pristine">pristine</span>
    <span> & </span>
    <span *ngIf="form.get('character')?.touched">touched</span>
    <span *ngIf="form.get('character')?.untouched">untouched</span>
    <div>Form value: {{form.value | json}}</div>
</div>
<form
    novalidate
    [formGroup]="form">
    <ng-select
        formControlName="character"
        [allowClear]="true"
        [options]="characters">
    </ng-select>
</form>
`; ts0: string = `
export class ReactiveFormExample {

    characters: Array<IOption> = this.optionService.getOptions();
    defaultCharacter: string = '3';

    constructor(
        private optionService: OptionService
    ) {}

    ngOnInit() {
        this.form = new FormGroup({
            character: new FormControl(this.defaultCharacter)
        });
    }
}
`; html1: string = `
<div>
    <span>Form state: </span>
    <span *ngIf="form.get('character')?.dirty">dirty</span>
    <span *ngIf="form.get('character')?.pristine">pristine</span>
    <span> & </span>
    <span *ngIf="form.get('character')?.touched">touched</span>
    <span *ngIf="form.get('character')?.untouched">untouched</span>
    <div>Form value: {{form.value | json}}</div>
</div>
<form
    novalidate
    [formGroup]="form">
    <ng-select
        formControlName="character"
        [allowClear]="true"
        [options]="characters">
    </ng-select>
</form>
`; ts1: string = `
export class ReactiveFormExample {

    characters: Array<IOption> = this.optionService.getOptions();
    defaultCharacters: Array<string> = ['1', '3'];

    constructor(
        private optionService: OptionService
    ) {}

    ngOnInit() {
        this.form = new FormGroup({
            character: new FormControl(this.defaultCharacters)
        });
    }
}
`; }