Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UI router same controller for multiple states

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

I have configured UI router as 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 route '/v1' is called, fv2 when route '/v2' is called.

Can somebody suggest?

like image 726
Kalyan Chakravarthy S Avatar asked Jul 16 '16 18:07

Kalyan Chakravarthy S


1 Answers

Inject $state into the controller, and check $state.current.name - It should be profile.v1 or profile.v2, depending on what state you're currently in

like image 180
Alon Eitan Avatar answered Oct 03 '22 12:10

Alon Eitan