I'm trying to create a custom form control component for a <select>
element (I realize this isn't the most innovative use of creating a custom form control, but this is just for testing purposes). I'm following along with the tutorial @ http://blog.thoughtram.io/angular/2016/07/27/custom-form-controls-in-angular-2.html.
What I'm aiming for is to have the submit button disabled until a value has been selected from the <select-box>
component, but I don't think I have the custom form control wired up properly as the value doesn't change when I select a different value nor does the validation work (validation = just a required
HTML attribute on the custom component).
See below for what I have so far. Alternatively a plunker is available at http://plnkr.co/edit/TAxDyb8sHg158dXmyfwr?p=preview.
Thanks!
Main component
import {Component, NgModule} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import {SelectBoxComponent} from "./select-box.component";
import {FormsModule} from "@angular/forms";
@Component({
selector: 'my-app',
template: `
<div>
<h2>Hello {{name}}</h2>
<form #form="ngForm" (ngSubmit)="log(form.value)">
<select-box name="someValue" [ngModel]="someValue" required></select-box>
<br>
<button type="submit" [disabled]="!form.valid">Submit</button>
</form>
<br>
{{ form.value | json }}
</div>
`,
})
export class App {
name:string;
someValue: any = 1;
log(str) {
console.log(str);
}
}
@NgModule({
imports: [ BrowserModule, FormsModule ],
declarations: [ App, SelectBoxComponent ],
bootstrap: [ App ]
})
export class AppModule {}
Select Box Component
import { Component, forwardRef, Input } from "@angular/core";
import { SelectControlValueAccessor, NG_VALUE_ACCESSOR } from "@angular/forms";
@Component({
selector: "select-box",
template: `
<select onchange="onChanged(event.target.value)" [ngModel]="ngModel">
<option disabled selected value></option>
<option value="1">1</option>
<option value="2">2</option>
</select>
`,
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => SelectBoxComponent),
multi: true,
}
],
})
export class SelectBoxComponent implements SelectControlValueAccessor {
@Input() ngModel: any;
onChanged(value) {
this.ngModel = value;
this.propagateChange(value);
}
writeValue(value: any) {
if (value) this.value = value;
}
propagateChange = (_: any) => {};
registerOnChange(fn) {
this.propagateChange = fn;
}
registerOnTouched() {}
}