Angularjs rootscope 在路由改变时不更新 ngClass

Angularjs rootscope not updating ngClass when route change

$routeChangeStart 我喜欢应用 of-h class 在 $routeChangeSuccess 我喜欢删除 of-h class 或将其更改为of-vclass。现在只有 of-h class 得到应用。

HTML

<div class="container-outer" ng-class="addClassOnRouteChange">
  <div class="container" ng-view autoscroll="true" ng-class="fadeNgView">
    <!-- Views will be rendered here -->
  </div>
</div>

JS

angular.module('starter', ['ngRoute', 'ngAnimate', 'myApp.controllers'])

  .run(['$window', '$location','$rootScope', function ($window, $location, $rootScope) {
    $rootScope.fadeNgView = '';

    $rootScope.$on('$routeChangeStart', function() {
      //event button item list to move forward
      $rootScope.next = function() {
        console.log('start');
        $rootScope.fadeNgView = 'fade-ng-view';
        $rootScope.addClassOnRouteChange = 'of-h';
      }
    });

    $rootScope.$on('$routeChangeSuccess', function (event, current, previous) {
      $rootScope.addClassOnRouteChange = 'of-v';
    });
  }]);

试试这个

$rootScope.$on('$routeChangeStart', function() {
  //event button item list to move forward
  $rootScope.addClassOnRouteChange = true;
  $rootScope.next = function() {
    console.log('start');
    $rootScope.fadeNgView = 'fade-ng-view';
    $rootScope.addClassOnRouteChange = true;
  }
});

$rootScope.$on('$routeChangeSuccess', function (event, current, previous) {
  $rootScope.addClassOnRouteChange = false;
});

现在将 ng-class 代码更改为

ng-class="{addClassOnRouteChange?'of-h':'of-v'}"

试试这个:

$rootScope.$apply(function(){
    $rootScope.addClassOnRouteChang‌​‌​e = 'of-h';
});