【发布时间】:2017-03-09 11:17:08
【问题描述】:
我一直在寻找这种行为的某种原因,子组件“Param”中的 Input() 属性的值没有在正确的时间更新,我需要使用更新后的值来调用服务一个参数。通过单击“更新子值”按钮,首先显示 LOG B(带有旧值),然后显示 LOG A(LOG A 它位于属性的设置器中)。
父组件
@Component({
selector: 'parent',
template: `
<child #child [param]="parentParam"></child>
<button (click)="updateChildValue()">Update child value</button>
`})
export class Parent {
@ViewChild('child') child: Child;
public parentParam: number;
public updateChildValue() {
this.parentParam = 9;
this.child.showParam();
}
}
子组件
@Component({
selector: 'child',
template: '' })
export class Child {
private _param: number;
public get param() : number {
return this._param;
}
@Input('param')
public set param(v : number) {
this._param = v;
console.log('LOG A');
console.log(this.param);
}
public showParam() {
console.log('LOG B');
console.log(this.param);
//I need to call a service using the latest value of param here...
}
}
期望的行为是首先更新参数的值,然后将该值用作服务的参数。 LOG A 然后 LOG B
【问题讨论】:
标签: angular angular2-components viewchild