1

I have a nested component in html side like following:

<main>
   <ng-content *ngFor="let item in data">
     <car *ngIf="item.type=='car'"></car>
     <plane *ngIf="item.type=='car'"></plane>
   </ng-content>
</main>

My car and plane components are implemented a base cehicle class.

export abstract class BaseVehicleComponent{
    onVehicleCreated(){
        console.log("on vehicle created.")
    }
}

When a vehicle created, I want to populate an event in main component.

@Component({
   selector: 'main',
})
export class MainComponent{

     @Output() vehicleCreated = new EventEmitter<void>();

}

And hml updated like this:

<main (vehicleCreated)="onVehicleCreated($event)">
   <ng-content *ngFor="let item in data">
     <car *ngIf="item.type=='car'"></car>
     <plane *ngIf="item.type=='car'"></plane>
   </ng-content>
</main>

but base methot does not fire.

4
  • Where are you calling this.vehicleCreated.emit()? Commented Sep 22, 2019 at 18:53
  • It would be helpful if you create an example on StackBlitz. Commented Sep 22, 2019 at 18:55
  • Actually nowhere, where can I cal?.Main component and vehicle components are seperated, and may be different modules. So I want to do a loosely coupled communication. Commented Sep 22, 2019 at 18:57
  • @barteloma - Check out my answer. Commented Sep 22, 2019 at 19:07

1 Answer 1

1

BaseVehicleComponent should have EventEmitter not MainComponent.

BaseVehicleComponent:

export abstract class BaseVehicleComponent{
   @Output() vehicleCreated = new EventEmitter<void>();
}

MainComponent should have onVehicleCreated() function not BaseVehicleComponent.

MainComponent:

export class MainComponent{  
  onVehicleCreated(){
    console.log("on vehicle created.")
  }
}     

In car or plane components' ngOnInit() method call event emitter's emit() method.

this.vehicleCreated.emit();

Modify MainComponent's template to be

<ng-container *ngFor="let item in data">
  <car *ngIf="item.type=='car' (vehicleCreated)="onVehicleCreated($event)"></car>
  <plane *ngIf="item.type=='plane' (vehicleCreated)="onVehicleCreated($event)"></plane>
</ng-container>

Live demo on StackBlitz: https://stackblitz.com/edit/angular-thvzue

For more info, check out my answer on communication between parent and child components.

Sign up to request clarification or add additional context in comments.

5 Comments

@barteloma - Check out my edited answer. You can also see this working on StackBlitz.
I put my html <main> and <ng-container> in app.component.html, so an error occured because of onVehicleCreated() not found.
No, just put <main> in app-component. You can see how I did it in the StackBlitz.
Can I put in app.component.html and fire onVehicleCreated() of mainComponent?
But then data variable should be placed in app-component. Also then what is the purpose of main-component when you are placing all the components directly in app-component?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.