[英]Data showing as undefined when I am trying to pass data from parent component to child components
I am trying to pass data from parent component to child components, but I am getting data is undefined
, In my below code我试图将数据从父组件传递给子组件,但我得到的数据
undefined
,在我下面的代码中
parent component父组件
here part_data I have declared in service这里 part_data 我已经在服务中声明
this._PartService.GetDataValues(this.search.item, this.search.filter_type, release_part).subscribe(
result => {
this.resultData = result;
this._PartService.part_data = this.resultData.data
})
child component子组件
Here I am using observer在这里我使用观察者
this.searchService.observer.subscribe(search => {
this.part = search.item;
this.specification = this._PartService.part_data.partMasterDataCompleteList[0];
})
Here I am getting PartMasterDataCompleteList[0] is undefined
在这里我得到
PartMasterDataCompleteList[0] is undefined
Use this as the child component:-将此用作子组件:-
@Component({
selector: 'app-hero-detail',
templateUrl: './hero-detail.component.html',
styleUrls: ['./hero-detail.component.css']
})
export class HeroDetailComponent implements OnInit {
@Input() hero: Hero;
constructor() {}
ngOnInit() {}
}
When you need to feed data to the component, use the below code:-当您需要向组件提供数据时,请使用以下代码:-
<app-hero-detail [hero]="selectedHero"></app-hero-detail>
The hero property is @Input
which receives data from the outer environment.英雄属性是
@Input
,它从外部环境接收数据。
For more information, refer to Angular tutorial:-有关更多信息,请参阅 Angular 教程:-
https://angular.io/tutorial/toh-pt3
https://angular.io/tutorial/toh-pt3
You can pass your data in sub component and get using @Input
method.您可以在子组件中传递数据并使用
@Input
方法获取。
There is one other way to pass data from one component to other component.还有另一种方法可以将数据从一个组件传递到另一个组件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.