3

We have component ( ka-cockpit-panel) which is not mapped to any route and inserted manually in the some other component as shown below :

..
...
<section class="ka-cockpit-panel cockpit-1 pull-left">
            <ka-cockpit-panel></ka-cockpit-panel>
</section>
...
..

In this component i want to access the current active route data.

For eg : if we have some other component ( say ka-integration-component ) and it has some route data associated with it ( as shown below ), whenever we navigate to this component ( via url or by clicking some routerlink ) , we want to access integration component route data in our ka-cockpit-component.

 ..
    ... 
    {       
        path: "",       
        component: IntegrationComponent,
        data : {
            cockpit1 : false,
            cockpit2 : true,
            kpi : true
        },  
    }
    ..
    ...

Basically, we want to configure our ka-cockpit-component for certain components in our app which is mapped to some route so that we can hide / show or change its appearance.



Cockpit component code :

import { Component, OnInit } from '@angular/core';
import { Router,Event,NavigationEnd,ActivatedRoute } from '@angular/router';

@Component({
    selector: 'ka-cockpit-panel',
    templateUrl: './cockpit-panel.component.html',
    styleUrls : ['./cockpit-panel.component.scss']
})
export class CockpitPanelComponent implements OnInit {

    constructor(private router:Router,private activatedRoute : ActivatedRoute) {
         this.router.events.subscribe( (event:Event) => {
            if(event instanceof NavigationEnd) {
                console.log("Cockpit Panel Component : Route successfully changed -  ",event,this.router,this.activatedRoute);

                  // THIS IS WHAT WE WANT - get  Integration component route data here whenever i navigate to integration component!!!

            }
        });
     }

    ngOnInit() { }
}
Community
  • 1
  • 1
bhavya_w
  • 9,186
  • 9
  • 29
  • 38

1 Answers1

0

You have to use Resolve Guard for the thing you want to implement.

// MyDataResolver Service

import { Injectable }             from '@angular/core';
import { Router, Resolve, RouterStateSnapshot,
         ActivatedRouteSnapshot } from '@angular/router';

@Injectable()
export class MyDataResolver implements Resolve<any> {
  constructor(private cs: CrisisService, private router: Router) {}
  resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<any> {

    let pathFromRoot = route.pathFromRoot;

    // you can compare pathFromRoot with your route to return different data

    return Promise.resolve({
        cockpit1 : false,
        cockpit2 : true,
        kpi : true
    });

  }
}

// Routing configuration

.
.
{       
    path: "",       
    component: IntegrationComponent,
    resolve : {
        data: MyDataResolver
    },  
}
.
.

// Component

export class CockpitPanelComponent implements OnInit {
  someBinding : string = "testing Value";

  constructor(private router:Router,private activatedRoute : ActivatedRoute) {

    this.activatedRoute.data.subscribe( (res) => {

      // here you will get your data from resolve guard.
      console.log(res);

    });
  }

  ngOnInit() { }
}
ranakrunal9
  • 13,320
  • 3
  • 42
  • 43
  • This doesn't work. "this.activatedRoute" has the route information of ka-cockpit-panel-component Parent and not the integration component. – bhavya_w Jan 12 '17 at 13:30
  • have you checked `this.activatedRoute.parent` which is parent in router state tree for current activated route, so i think check with `this.activatedRoute.parent.data.subscribe((res) => { console.log(res); })` – ranakrunal9 Jan 12 '17 at 13:34
  • The type annotation `Promise ` is a poor choice. Just leave it off completely and you'll get a better type. – Aluan Haddad Jan 14 '17 at 04:09