【发布时间】:2018-07-13 19:12:59
【问题描述】:
我使用自定义组件字段。
在我尝试将模型替换为另一个模型之前,一切正常。
每个字段的组件不会更新为新值。
也没有开启任何事件
我尝试改变模型的方式:
this.model = this.data.model[this.currentRow];
html formly 标记
<form class="formly" role="form" [formGroup]="form" >
<formly-form #formly [(model)]="model" [fields]="fields" [form]="form" [options]="options">
</formly-form>
</form>
</div>
app.model.ts
FormlyModule.forRoot({
types: [
{ name: 'customTextNumeric', component: VerticalNumericEditorComponent},
{ name: 'customDatepicker', component: VerticalDatePickerComponent },
{ name: 'customSelect', component: VerticalSelectComponent },
.
。 .
我的自定义组件(例如)
import { Component, OnInit, Input, ViewChild, SimpleChanges } from '@angular/core';
import { FieldType } from '@ngx-formly/core';
import { FormControl, FormGroupDirective, NgForm, Validators } from '@angular/forms';
import { ErrorStateMatcher } from '@angular/material/core';
import { SearchLabelService } from '../../tableHandle/search-label.service';
/** Error when invalid control is dirty, touched, or submitted. */
export class MyErrorStateMatcher implements ErrorStateMatcher {
isErrorState(control: FormControl | null, form: FormGroupDirective | NgForm | null): boolean {
const isSubmitted = form && form.submitted;
return !!(control && control.invalid && (control.dirty || control.touched || isSubmitted));
}
}
@Component({
selector: 'app-vertical-numeric-editor',
templateUrl: './vertical-numeric-editor.component.html',
styleUrls: ['./vertical-numeric-editor.component.css']
})
export class VerticalNumericEditorComponent extends FieldType {
private validates = [];
private initialValue;
private value;
private validateValues = { required: false, minLen: 0, maxLen: 99999999, pattern: '' };
constructor(private labelSearch: SearchLabelService) {
super();
this.numericFormControl = new FormControl('', this.validates);
console.log('constructor field:');
}
numericFormControl: FormControl;
onInit() {
console.log('oninit');
}
matcher = new MyErrorStateMatcher();
ngOnInit() {
}
OnChanges(e) {
console.log('on changes');
}
ngDoCheck() {
}
ngOnDestroy() {
}
get type() {
return this.to.type || 'text';
}
}
有人知道如何使用新值更新组件
【问题讨论】:
标签: javascript angular typescript angular-cli angular-formly