我只需要在输入值更改时更新子组件,子组件具有一些输入参数,这些输入参数会在ngInit()内设置一些局部变量,但问题是父级,而更改子参数不会更新的输入参数是因为ngInit()仅在初始化后才触发。
内部父母
<app-site-parameter-tbl *ngIf="siteTbl==true" [siteParameterTDataSet]="siteParameterDataSet" [siteParameterTGridColumns]="siteParameterColumns" [siteParameterFoot]="siteParameterFooter" [siteParameterT]="siteParameterTbl" [model]="siteModel" [bckColor]="this.tabBackColor"></app-site-parameter-tbl>
在某些触发事件上,父级更改了这些属性(作为子级的输入)
父母Ts
method(ds,columns,footer)
{
this.siteParameterDataSet = ds;
this.siteParameterColumns = columns;
this.siteParameterFooter = footer;
}
即使更改输入值也仅命中一次
Child ts
@Input() siteParameterT:boolean;
@Input() model :SiteModel;
@Input() siteParameterFoot:[];
@Input() siteParameterTGridColumns:[];
@Input() siteParameterTDataSet:any;
ngOnInit() {
debugger;
//setting local variables then they will render inside html
this.siteParameter = this.siteParameterT;
this.site = this.model;
this.foot = this.siteParameterFoot;
this.dataSet = this.siteParameterTDataSet;
}
[请帮助,如果我可以直接在html中使用Input属性,那么我怎么能对其进行更改?帮助我知道输入更改时如何通知吗?
@Input
上使用设置器:@Input('siteParameterT')
set siteParameterT(value: boolean) {
this.siteParameter = value
}
也,您可以使用ngOnChanges
示例用法
@Input() employee: Employee
ngOnChanges(changes: SimpleChanges) {
/** Fire any time employee changes */
if (changes.employee && changes.employee.currentValue) {
this.formGroup.patchValue(this.employee)
}
}
ngOnChanges
实现功能。 public ngOnChanges(changes: SimpleChanges): void {
this.siteParameter = changes.siteParameter;
etc.,
}