@Output() onChange = new EventEmitter();
<select select2 class="select2" (onChange)="setSelect($event)" formControlName="test3">
...
import { Component, Input } from "@angular/core";
import { NG_VALUE_ACCESSOR, ControlValueAccessor } from "@angular/forms";
@Component({
selector: 'test-input',
template: `<input type="text" [(ngModel)]="value" (blur)="onBlur()">`,
providers: [{provide: NG_VALUE_ACCESSOR, useExisting:TestInputComponent, multi: true}],
})
export class TestInputComponent implements ControlValueAccessor {
private innerValue: any = '';
private onTouchedCallback: () => void = () => {};
private onChangeCallback: (_: any) => void = () => {};
get value(): any { return this.innerValue; };
set value(v: any) {
if (v !== this.innerValue) {
this.innerValue = v;
this.onChangeCallback(v);
}
}
onBlur() { this.onTouchedCallback(); }
writeValue(value: any) {
if (value !== this.innerValue)
this.innerValue = value;
}
registerOnChange(fn: any) { this.onChangeCallback = fn; }
registerOnTouched(fn: any) { this.onTouchedCallback = fn; }
// ... Другие любые действия которые нам нужны
}
<form [formGroup]="form">
<test-input formControlName="test"></test-input>
</form>
import { FormGroup, FormBuilder } from '@angular/forms';
@Component({
...
})
export class ComponentName {
form: FormGroup;
constructor(
private fb: FormBuilder
){
this.form = this.fb.group({
test3: [...]
});
}
}
setSelect(event) {
this.form.controls['test3'].setValue(event);
}