1

I work on the team who using Angular, I was using Vue before, I have learned the Angular 7 so far for 5 days, and my team still using Angular 4, I want to know how to set up the prop in child to parent? I usually in Vue use Prop in components so, I can use it on other component and set the data of Prop, but in Angular, I don't know how to use it (thought I have learned it so far and still not sure how to binding in the correct way) and is it possible ??

I want to make a loader component in this Angular component, but I want to make it reusable to set up the width and height of the loader on another component, how to do this in Angular ??

let say I want to fetch data and then wait for it while showing a loader

like this :

<div>
   <app-loader *ngIf="show == 'true'" [setupWidhtLoader]="is-That-Possible-to-Set-Up-Here?"> </app-loader>
   <h2> {{ data.full_name  }} </h2>
</div> 
R. Richards
  • 24,603
  • 10
  • 64
  • 64
Zum Dummi
  • 243
  • 1
  • 11

1 Answers1

1

To pass data from the child component to the parent via an event, use the @Output() decorator. See the docs for more details. An example is shown below:

Parent HTML component:

<app-loader (dataChange)="handleDataChange($event)"></app-loader>

Parent TS component:

public handleDataChange(event){
  //Do something here
}

Child TS component:

@Output()
public dataChange:EventEmitter<any> = new EventEmitter<any>();

You can also pass data back and forth between the parent and child component using two-way binding. When the value is changed in one location, it will automatically be changed in the other. Syntax is as follows:

<app-loader [(width)]="parentWidth"></app-loader>

Where parentWidth is a property on the parent and width is a property on the child decorated with @Input() and widthChange is an EventEmitter property of the child decorated with @Output().

If you want to share data between these two components and potentially others, you can create a shared service. This is probably a little overkill for your situation.

Joe Newton
  • 449
  • 1
  • 5
  • 11
  • i see that, now i get some error, i want to use that child component in my current component, it not going trigger :( – Zum Dummi May 17 '19 at 17:58