Angular 子组件上的触发器更改检测
假设我正在测试Angular 子组件上的触发器更改检测,angular,jasmine,Angular,Jasmine,假设我正在测试MyComponentwho的模板包含SubComponent MyComponent看起来像这样 @Component({ selector: 'my-component', template: ` <sub-component [input]="property"></sub-component> ` }) export class MyComponent implements OnInit { property: string
MyComponent
who的模板包含SubComponent
MyComponent看起来像这样
@Component({
selector: 'my-component',
template: `
<sub-component [input]="property"></sub-component>
`
})
export class MyComponent implements OnInit {
property: string;
constructor() { }
ngOnInit() {
this.property = 'value passed to sub component';
}
}
@组件({
选择器:“我的组件”,
模板:`
`
})
导出类MyComponent实现OnInit{
属性:字符串;
构造函数(){}
ngOnInit(){
this.property='传递给子组件的值';
}
}
当我测试MyComponent
并执行comp.ngOnInit()
时,子组件
不会检测到输入的更新值
如何让子组件
检测到该更改?您可以在您的子组件中使用生命周期挂钩。这是否有效?我没有看到@Input
注释,即使有注释,您也在您的孩子中使用了错误的属性名称(property
)。