Home Is there a way to distinguish whether the state is being triggered directly or through a child?
Reply: 1

Is there a way to distinguish whether the state is being triggered directly or through a child?

Nat
1#
Nat Published in 2018-01-12 14:21:56Z

I need to find a way to know whether the user is navigating to a parent state directly or the state is being activated because a child is being activated.

Let's say I have these states:

var main = {
   name: 'main',
   url: '/main',
   component: 'main'
}

var child = {
   name: 'main.child',
   url: '/child',
   component: 'child'
}

I have a function in main component's controller that I only want to run when the user has navigated www.mysite.com!#/main and not run it if the user clicked on a url www.mysite.com!#/main/child

Is it possible to achieve?

I am using AngularJS and the latest version on router-ui.

Nat
2#
Nat Reply to 2018-01-22 07:37:12Z

I found a pretty simple and obvious solution that works by checking $state.current.name. Don't know why I haven't though of it earlier.

My solution is something along these lines

ctrl.$onInit = function(){
   if($state.current.name === 'main'){
       // do something
   } 
   else if ($state.current.name === 'main.child'){
       // do something else
   }
   else {
       // do completely something else
   }
}

Maybe this helps somebody in the future.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.304103 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO