UI路由器用于多个状态的控制器

Kal*_*y S 7 javascript angularjs single-page-application angular-ui-router angular-ui-router-extras

在我的Angular JS应用程序中,我有2个视图 - v1和v2以及1个控制器 - appCtrl.

我已经配置了如下的UI路由器

          .state('profile.v1', {
            url: '/v1',
            templateUrl: 'v1.html',
            controller: 'appCtrl'
        })
          .state('profile.v2', {
            url: '/v2',
            templateUrl: 'v2.html',
            controller: 'appCtrl'
        })
Run Code Online (Sandbox Code Playgroud)

我在appCtrl中有2个函数--fv1和fv2.

我想在调用route'/ v1'时执行fv1,在调用route'/ v2'时执行fv2.

有人可以建议吗?

Alo*_*tan 5

注入$state控制器,然后检查$state.current.name-应该为profile.v1或profile.v2,具体取决于您当前所处的状态