Настройка модуля
@NgModule({
imports: [
BrowserModule,
ReactiveFormsModule,
FormlyModule.forRoot({
types: [
{ name: 'customInput', component: FormlyFieldInput },
]
}),
],
declarations: [ AppComponent, FormlyFieldInput ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
Настройка пользовательского компонента и прослушивание keyup
@Component({
selector: 'formly-field-input',
template: `
<div>{{this.model.profilePictureNotEditable}}</div>
<input type="text" [formControl]="formControl" [formlyAttributes]="field">`,
})
export class FormlyFieldInput extends FieldType implements OnInit {
val;
constructor() {
super();
}
ngOnInit() {
console.log(this.key);
console.log(this.model)
}
}
Настройка формы в app.component
@Component({
selector: 'my-app',
template: `
<form [formGroup]="form" (ngSubmit)="submit(model)">
<formly-form [model]="model" [fields]="fields">
<button type="submit">Submit</button>
</formly-form>
</form>
{{ model|json }}
`,
})
export class AppComponent {
form = new FormGroup({});
model = {profilePictureNotEditable: 'John'};
fields: FormlyFieldConfig[] = [
{
fieldGroup: [
{
key: 'name',
type: 'customInput',
templateOptions: {
label: 'Field 1',
type: 'text',
placeholder: 'Formly is terrific!',
},
}]
}];
submit(model) {
console.log(model);
this.model.profilePictureNotEditable = 'this will be the profile picture!'
}
}
https://stackblitz.com/edit/ngx-formly-custom-template-ydrfss Надеюсь, это поможет вам !!