San Dra
San Dra

Reputation: 3

Angular If statement- check route

I want to do an if-statement for my program to check on which route/ path I'm currently on.

I have a function and this function should only work when I'm on a specific path. For example: If the path is homepage I'm on the "homepage", but if I write "homepage/add" in the url, I want to call a function. I tried " if(this.router.url.include('add') { .... code ... } but It didn't work. Could someone help me? Thank you!

Upvotes: 0

Views: 564

Answers (1)

Dmitry Sobolevsky
Dmitry Sobolevsky

Reputation: 1191

You have to add nested route '/add' and then in this component which will be available for 'add' route - call your function on ngOnInit(){ /// }.

const routes: Routes = [
  {
    path: 'homepage',
    component: DashboardComponent,
    children: [
      {path: 'add', component: DashboardAddComponent}
    ]
  }
]

then in component:

class DashboardAddComponent implements OnInit {
  ngOnInit() {
    // ... CALL HERE
  }
}

Upvotes: 1

Related Questions