从 ng-route 更改为 ui-router $routeChangeSuccess 无效

Changing from ng-route to ui-router $routeChangeSuccess is not working

$scope.$on("$routeChangeSuccess", function (event, next, current) {
                switch (next.originalPath) {
                    case "/ac" :
                        $scope.currentTab = 1;
                        break;
                    case "/bp/:type?/:tab?":
                        $scope.currentTab = 2;
                        break;
                    case "/bs":
                        $scope.currentTab = 3;
                        break;
                    default :
                        $scope.currentTab = 1;
                        break;
                }
            });

我想将上面的代码从 ng-route 更改为 ui-router。我将其他人更改为 stateProvider 和 stateParams 但不知道 $routeChangeSuccess 将如何更改为 $stateChangeSucess?

$rootScope.$on('$stateChangeSuccess', 
function(event, toState, toParams, fromState, fromParams){ ... })

根据 State Change Events.

,您应该使用 $stateChangeSuccess 而不是 $routeChangeSuccess

$stateChangeStart - fired when the transition begins.

$rootScope.$on("$stateChangeSuccess", function () {

            });

是的,$stateChangeSuccess。从这里查看更多 https://github.com/angular-ui/ui-router/wiki

$scope.$on("$stateChangeSuccess", function (event, toState, toParams, fromState, fromParams) {
                switch ( toState.url ) {
                    case "/ac" :
                        $scope.currentTab = 1;
                        break;
                    case "/bp":
                        $scope.currentTab = 2;
                        break;
                    case "/bs":
                        $scope.currentTab = 3;
                        break;
                    default :
                        $scope.currentTab = 1;
                        break;
                }
            });

toState.url 将给出更改后的 url 并可以执行与此相关的操作。