简体   繁体   English

当我尝试将数据从父组件传递到子组件时数据显示为未定义

[英]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.还有另一种方法可以将数据从一个组件传递到另一个组件。

Click here 点击这里

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试将数据从子组件传递到父组件时出现错误“ NavController没有提供者” - Getting the error “No provider for NavController” when trying to pass data from child component to parent component 在Vue js中将数据从父组件传递给子组件 - Pass data from parent component to child of child in Vue js React 多次将数据从同一个子组件传递给父组件 - React pass data from same child to parent component multiple times 如何将数据从子 Web 组件传递到它的直接父元素 - How do i pass data up from a child web component to it's direct parent element Angular 2将数据父级传递给子级组件 - Angular 2 pass data parent to child component 使用角度8中的@input装饰器将数据从父组件传递到子组件 - Pass data from parent component to child component using @input decorator in angular 8 使用输入从父组件向子组件传递数据,如何实现? - Pass data from parent component to child component using input, how to implement? 将数据参数从父级传递给子级 - Pass data-parameters from parent to child 将用户输入的FORM数据发送到同一组件中的弹出窗口(不使用子/父组件或组件之间的组件)? - Send User entered FORM data to a Popup within same component (without using child/parent components or component to component)? 在 Angular 中将数据从父组件传递到子组件时出现 HTML 错误 - HTML error when Passing data from Parent component to Child Component in Angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM