Angular 在ngOnInit中使用@Input
我已使用Angular 在ngOnInit中使用@Input,angular,Angular,我已使用@Input将应用程序对象从父组件传递到子组件。现在我尝试在子组件的ngOnInit上使用它。该对象仅在ngOnChanges上可用,因此无法在ngOnInit上使用。尽管如此,我还是尝试了下面这样的方法,但无法成功 组件 @Input()应用程序; appModified; 列表详细信息(){ this.appService.getDetails(this.appModified); } ngOnChanges(){ console.log(this.app) this.appModi
@Input
将应用程序对象从父组件传递到子组件。现在我尝试在子组件的ngOnInit
上使用它。该对象仅在ngOnChanges
上可用,因此无法在ngOnInit
上使用。尽管如此,我还是尝试了下面这样的方法,但无法成功
组件
@Input()应用程序;
appModified;
列表详细信息(){
this.appService.getDetails(this.appModified);
}
ngOnChanges(){
console.log(this.app)
this.appModified=this.app;
}
恩戈尼尼特(){
这是listDetails();
}
listDetails未定义
我试过了,但还是修不好
感谢您的帮助。谢谢你@surazzarus
确保html模板与下面类似,其中someString
content将实例绑定到子对象的name
属性:
<hello [name]="someString"></hello>
最后一段代码:
import { Component, Input } from '@angular/core';
@Component({
selector: 'hello',
template: `<span>{{name}}</span>`
})
export class HelloComponent {
@Input() name: string;
ngOnInit() {
console.log(this.name);
}
ngOnChanges() {
console.log(this.name)
}
}
从'@angular/core'导入{Component,Input};
@组成部分({
选择器:“你好”,
模板:`{name}}`
})
导出类HelloComponent{
@Input()名称:string;
恩戈尼尼特(){
console.log(this.name);
}
ngOnChanges(){
console.log(this.name)
}
}
希望对你有帮助
检查整个样本调用this.listDetails()
在ngAfterViewInit()
@Faisal中,我也尝试过,但没有成功ngoninit(){setTimeout(()=>{this.listDetails();},0}您知道可以多次调用ngOnChanges,对吗?如果您检查父组件中的输入数据,那么它将在数据满时填充数据,
这有什么问题?
import { Component, Input } from '@angular/core';
@Component({
selector: 'hello',
template: `<span>{{name}}</span>`
})
export class HelloComponent {
@Input() name: string;
ngOnInit() {
console.log(this.name);
}
ngOnChanges() {
console.log(this.name)
}
}