sneha mahalank
sneha mahalank

Reputation: 167

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

parent component

here part_data I have declared in service

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

Upvotes: 0

Views: 1272

Answers (2)

Ghanshyam
Ghanshyam

Reputation: 265

You can pass your data in sub component and get using @Input method.

There is one other way to pass data from one component to other component.

Click here

Upvotes: 0

Ankit Sharma
Ankit Sharma

Reputation: 1704

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.

For more information, refer to Angular tutorial:-

https://angular.io/tutorial/toh-pt3

Upvotes: 1

Related Questions