Configuration items are defined in 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']);
});
});
Define modules and routes in 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 code is as follows
define(['app'], function (yceMain){
yceMain.controller('appManage', ['$scope' , function ($scope){
$scope.aa = 11;
}]);
});
Page error
Error: [ng:areq] http://errors.angularjs.org/1.5.0/ng/areq?p0=appManage&p1=not%20aNaNunction%2C%20got%20undefined
If you define appManage controller in app.js, you can get the data in the view, but if you use define(['app'],xx) in appManage controller.js to get angular.module, you cannot get it. Please solve it.
我想大声告诉你2017-05-15 17:10:36
define(['app'], function (yceMain){
yceMain.controller('appManage', ['$scope' , function ($scope){
$scope.aa = 11;
}]);
});
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:
// appManage controller.js
angular.module('app')
.controller('appManage', ['$scope' , function ($scope){
$scope.aa = 11;
}]);
Of course, this controller must also be introduced into the page
I used ui-router + oclazyload
某草草2017-05-15 17:10:36
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.
function ctrlRegister (ctrlName, ctrlModule) {
return function ($q) {
var defer = $q.defer();
require(ctrlModule, function (controller) {
$controllerProvider.register(ctrlName, controller);
defer.resolve();
});
return defer.promise;
}
}
This function solves the problem perfectly, thanks /a/11….