main.js中定义好配置项
requirejs.config({
paths: {
jQuery: '../lib/jquery.min',
angular: '../lib/angular.min',
angularRouter: '../lib/angular-route.min',
angularAnimate: '../lib/angular-animate.min',
ngNotify: '../lib/angular-atomic-notify',
api: '../js/common/api',
utils: '../js/common/utils'
},
shim: {
'angular': {
deps: ['jQuery'],
exports: 'angular'
},
angularRouter: {
deps: ['angular'],
exports: 'ngRoute'
},
angularAnimate: {
deps: ['angular']
},
ngNotify: {
deps: ['angular']
},
api: {
deps: ['angular']
},
utils: {
deps: ['angular']
}
}
});
// 手动开启angular服务
require(['angular', 'app'], function(angular){
angular.element(document).ready(function(){
angular.bootstrap(document, ['yceMain']);
});
});
app中定义模块与路由
app.js
define(['angular', 'angularRouter', 'ngNotify', 'angularAnimate', 'api', 'utils'], function(angular){
var yceMain = angular.module('yceMain',['ngRoute', 'atomic-notify', 'ngAnimate']);
yceMain.config(['$routeProvider', '$controllerProvider', function ($routeProvider, $controllerProvider){
$routeProvider
.when('/dashBoard', {
templateUrl: 'views/dashBoard.html'
})
.when('/appManage', {
templateUrl: 'views/appManage.html',
controller: 'appManage'
})
}]);
return yceMain;
});
appManage controller.js代码如下
define(['app'], function (yceMain){
yceMain.controller('appManage', ['$scope' , function ($scope){
$scope.aa = 11;
}]);
});
页面报错
Error: [ng:areq] http://errors.angularjs.org/1.5.0/ng/areq?p0=appManage&p1=not%20aNaNunction%2C%20got%20undefined
若在app.js中定义appManage controller,则视图中可以拿到数据,但如果在appManage controller.js 使用define(['app'],xx) 获取angular.module则取不到,求解。
This app should not be injected, please console.log(yceMain).
If you just want to bind the appManage controller to the app module, you should be able to write like this:
Of course, this controller must also be introduced into the page
I used ui-router + oclazyload
Solved. The problem is that before angular starts the ng-app service, all controllers and services must be registered.
But my idea is to load the controller asynchronously through require.
This function solves the problem perfectly, thanks /a/11….