olivier
olivier

Reputation: 159

pass data from child routed component to parent component

Starting in angular, I am faced with the following difficulty: I have to pass a data from a child routed component to a parent component.

I thought I could use @Output with a eventEmitter. But I feel like it’s not possible I also tried to use a topic that generates a change detection error (an ExpressionChangedAfterItHasBeenCheckedError).

ts of child compo :

 ngOnInit(): void {
    this.itemsMenu = this.rayonService.itemsMenu;
    this.activatedRoute.paramMap.subscribe(params => {
      let paramUrlZone = params.get('id');

      if (paramUrlZone !== null) {
        this.isTest = true; // pass to the parent
      }
    });
    this.initRefForm();
  }

html parent (child compo is routed...)

------------------------------------
      <div class="router-container">
        <router-outlet></router-outlet>
      </div>
------------------------------------

I don't how to do the stuff.. Thanks for your help !

Upvotes: 0

Views: 534

Answers (1)

Logan Young
Logan Young

Reputation: 385

You can avoid passing id to the parent by directly subscribing to the child's params data in the parent component like this:

constructor(route: ActivatedRoute) {
  route.firstChild.params.subscribe((params) => {
    console.log(params.id);
  });
}

Upvotes: 1

Related Questions