UI router - same controller for multiple states

In my Angular JS application, I have 2 views - v1 and v2 and 1 controller - appCtrl.

I configured the UI router below

.state('profile.v1', { url: '/v1', templateUrl: 'v1.html', controller: 'appCtrl' }) .state('profile.v2', { url: '/v2', templateUrl: 'v2.html', controller: 'appCtrl' }) 

I have 2 functions in appCtrl - fv1 and fv2.

I want to execute fv1 when the route '/ v1' is called, fv2 when the route '/ v2' is called.

Can anyone suggest?

+6
source share
2 answers

Paste $state into the controller and check $state.current.name - it should be profile.v1 or profile.v2 , depending on what state you are currently in

+3
source

Another way you can do this with a single state declaration:

 .state('profile.detail', { url: '/:version', templateUrl: function($stateParams){ return $stateParams.version +'.html'; }, controller: 'appCtrl' }) 

Then enter $ stateParams into the controller and check $stateParams.version

+3
source

All Articles