在AngularJS ui路由器上无法调用$ rootScope。$中的函数

时间:2018-03-07 11:56:37

标签: angularjs routing angular-ui-router yeoman

我在AngularJS中使用$rootScope.$on时遇到问题,我尝试调用alert,但是不成功,控制台没有任何错误,我生成这个项目使用yeoman生成器,我不知道为什么,这是我的脚本,请更正我的剧本。

'use strict';
angular
  .module('siapApp', [
    'ngAnimate',
    'ngCookies',
    'ngResource',
    'ngRoute',
    'ngSanitize',
    'ngTouch',
    'ui.router',
    'ui.bootstrap'
  ])
  .config(['$stateProvider', '$locationProvider', '$urlRouterProvider', '$qProvider', 
    function ($stateProvider, $locationProvider, $urlRouterProvider, $qProvider) {
      $stateProvider
        .state('main', {
            url: '/',
            views: {
                'content@': {
                    templateUrl: 'views/main.html',
                    controller: 'MainCtrl'
                }
            }            
        })
        .state('about', {
            url: '/about',
            views: {
                'content@': {
                    templateUrl: 'views/about.html',
                    controller: 'AboutCtrl'
                }
            }            
        })

        $locationProvider.hashPrefix('');
        $urlRouterProvider.otherwise('/');
        $qProvider.errorOnUnhandledRejections(false);
    }])

  .run(['$rootScope', '$state', '$stateParams',
    function($rootScope, $state, $stateParams) {

      $rootScope.$on('$stateChangeStart', function(event, toState, toStateParams) {
        alert('success'); /*cannot call alert*/
      });

    }
  ]);

请有人帮助我。

感谢。

1 个答案:

答案 0 :(得分:0)

这应该适用于任何情况。我唯一可以怀疑的是你的index.html页面上没有<ui-view></ui-view> 指令。

User user=new User();
String birthday = user.getBday();
相关问题