Angular js $ rootScope。$ on根本没有达到,好像它从未存在过

时间:2017-09-19 22:06:20

标签: javascript angularjs angularjs-scope

当我将console.log移动到rootscope.on上方时,它会打印出来,但由于某种原因,就好像根本没有达到rootscope.on一样:

'use strict';

var app = angular.module('myApp', ["ui.router"]);

app.config(function($stateProvider, $urlRouterProvider) {

  $urlRouterProvider.otherwise('/login');

  $stateProvider
    .state('app', {
      template: "<ui-view></ui-view>",
      abstract: true
    })
    .state('app.login', {
      url: '/login',
      controller: 'loginCtrl',
      templateUrl: 'content/auth/login.html',
      requireLogin: false,
      authorized: true
    })

    .state('app.register', {
      url: '/register',
      controller: 'registerCtrl',
      templateUrl: 'content/auth/register.html',
      requireLogin: true,
      authorized: true
    });
});


app.run(function($rootScope, $state) {

  $rootScope.$('$stateChangeStart', function(event, toState) {

    console.log("redirecting");

  })

});

1 个答案:

答案 0 :(得分:0)

您实际上并未拨打$on

更改

 $rootScope.$('$stateChangeStart', function(event, toState) {

    console.log("redirecting");

  })

  $rootScope.$on('$stateChangeStart', function(event, toState) {

    console.log("redirecting");

  })