在配置Angular 路由的时候,和以往一样使用如下配置:
- router.js
1 | var adminApp = angular.module('adminApp', ['oc.lazyLoad', 'ui.router']); |
- dashboardController.js
1
2
3angular.module("adminApp").controller('dashboardController', function ($scope) {
console.log("dashboardController");
});但是提示错误:
1 | angular.min.js:118 Error: [ng:areq] http://errors.angularjs.org/1.5.8/ng/areq?p0=dashboardController&p1=not%20aNaNunction%2C%20got%20undefined |
原因是因为直接写controller无法识别,所有需要使用register来注册该controller
- router.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28var adminApp = angular.module('adminApp', ['oc.lazyLoad', 'ui.router']);
angular.element(document).ready(function () {
angular.bootstrap(document, ['adminApp']);
});
adminApp.run(function ($rootScope, $state, $stateParams) {
$rootScope.$state = $state;
$rootScope.$stateParams = $stateParams;
});
adminApp.config(function ($stateProvider, $urlRouterProvider, $controllerProvider) {
//以下是新加入的
adminApp.controllerProvider = $controllerProvider;
$urlRouterProvider.when("", "dashboard/accountManagement");
$urlRouterProvider.otherwise("dashboard/accountManagement");
$stateProvider
.state('dashboard', {
url: '/dashboard',
templateUrl: 'dashboard/dashboard.html',
controller: 'dashboardController',
resolve: {
deps: ['$ocLazyLoad', function ($ocLazyLoad) {
return $ocLazyLoad.load(['dashboard/dashboard.js']);
}]
}
});
}); - dashboardController.js
1
2
3angular.module("adminApp").controllerProvider.register('dashboardController', function ($scope) {
console.log("dashboardController");
});