Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to check active child router from parent router or parent router against?

How to possible check Children Router Active Or Not, Show The status true or false in angular 4,
Now currently I'm use : /* @angular/cli: 1.4.4 node: 8.6.0 typescript: 2.3.4 @angular/router: 4.4.4 */

my Parent route is:

const routes:Routes=[
    {
        path: '', component: SummaryOfFindingsComponent,
        children:[
            {
                path:'data-time-frame', component: DataTimeFrameComponent
            },
            {
                path:'email-address', component: EmailAddressesComponent
            },
            {
                path:'repair-orders', component: RepairOrdersComponent
            },
            {
                path:'total-records', component:TotalRecordsComponent
            },
            {
                path:'unique-households', component: UniqueHouseholdsComponent
            },
            {
                path:'unique-vins', component: UniqueVinsComponent
            }
        ]
    }
]

Parent component is :

export class SummaryOfFindingsComponent implements OnInit {
    isUserSelected;
    constructor() { this.isUserSelected=false; }
    ngOnInit() { }
    isUserItemSelect(){
        this.isUserSelected=true;
    }
}
like image 401
Md. Abu Sayed Avatar asked Oct 13 '17 04:10

Md. Abu Sayed


2 Answers

Import your parent component.ts

import { ActivatedRoute } from '@angular/router';

and life cycle hooks ngOnInit() And create Refarence Of ActivatedRoute

constructor(private activeRouter:ActivatedRoute) {}

And Write Some code in your ngOnInit functions here..

ngOnInit() {
    var _activeChild = this.activeRouter.children.length;
    if (_activeChild!=0) {
        //your active children 1 or more than children then active 1,otherwise it is 0
    }
}

Note: This code is working my app ( Thank You )

like image 179
santo islam Avatar answered Sep 27 '22 23:09

santo islam


Here is a way to watch for children in an Observable...

this.router.events.pipe(
  filter(event => event instanceof ActivationEnd),
  filter(event => (event as ActivationEnd).snapshot.component === SearchFeatureComponent),
  map(event => (event as ActivationEnd).snapshot.children.length),
  distinctUntilChanged()
).subscribe(children => console.warn('route children', children))
like image 33
Jeff Barnes Avatar answered Sep 27 '22 23:09

Jeff Barnes