I'm trying to set up my angularjs app using AngularAMD (a Requirejs implementation with AngularJs), but sometimes i get
Cannot read property 'bootstrap' of undefined(…)
main.js
require.config({ paths: { //Angular 'angular': '/bower_components/angular/angular', 'angular-route': '/bower_components/angular-route/angular-route.min', 'angularAMD': '/bower_components/angularAMD/angularAMD.min', 'angular-slick': '/bower_components/angular-slick/dist/slick.min', 'angular-sanitize': '/bower_components/angular-sanitize/angular-sanitize', //Plugins 'jquery': '/bower_components/jquery/dist/jquery', 'materialize': '/bower_components/Materialize/dist/js/materialize.min', 'jquery-hammer': '/bower_components/Materialize/js/jquery.hammer', 'hammerjs': '/bower_components/Materialize/js/hammer.min', 'parallax': '/bower_components/parallax.js/parallax.min', 'slick': '/bower_components/slick-carousel/slick/slick.min', 'domReady': '/bower_components/requirejs/domReady', //Controllers 'HomeController': 'Components/Home/home.controller', }, shim: { angular: { exports: "angular", }, 'angular-route': ['angular'], 'angularAMD': ['angular'], 'angular-slick': ['angular', 'slick'], 'angular-sanitize': ['angular'], 'materialize': ['jquery', 'jquery-hammer', 'hammerjs'], 'parallax': ['jquery'], }, baseUrl: "src", deps: ['app'] }); });
app.js
define(['angularAMD', 'angular-route', 'angular-sanitize', 'angular-slick'], function(angularAMD) { var app = angular.module("app", ['ngRoute', 'ngSanitize', 'slick']); app.config(function($routeProvider, $locationProvider) { $routeProvider.when("/", angularAMD.route({ templateUrl: 'views/Home/home.html', controller: 'HomeController', controllerAs: 'vm' })); $locationProvider.html5Mode(true); }); return angularAMD.bootstrap(app); });
I've tried many differents ways, but none of them seems to work, what am i doing wrong?..
Note: it only happens sometimes, when the page loads..
Well since my last question was deleted, I was able to fix this issue. First: This maybe my neurosis, but add baseUrl before your paths array.
Second: (In your app.js), instead of
app.config(function($routeProvider, $locationProvider) {
$routeProvider.when("/", angularAMD.route({
templateUrl: 'views/Home/home.html',
controller: 'HomeController',
controllerAs: 'vm'
}));
$locationProvider.html5Mode(true);
});
return angularAMD.bootstrap(app);
Do this:
var app = angular.module("app", ['ngRoute', 'ngSanitize', 'slick']);
app.init = function () {
angular.bootstrap(document, ['app']);
};
return app;
This will load it to the DOM. If you have several controllers do so like this.
require.config({
baseUrl: "src",
paths: {
//Angular
'angular': '/bower_components/angular/angular',
'angular-route': '/bower_components/angular-route/angular-route.min',
'angularAMD': '/bower_components/angularAMD/angularAMD.min',
'angular-slick': '/bower_components/angular-slick/dist/slick.min',
'angular-sanitize': '/bower_components/angular-sanitize/angular-sanitize',
//Plugins
'jquery': '/bower_components/jquery/dist/jquery',
'materialize': '/bower_components/Materialize/dist/js/materialize.min',
'jquery-hammer': '/bower_components/Materialize/js/jquery.hammer',
'hammerjs': '/bower_components/Materialize/js/hammer.min',
'parallax': '/bower_components/parallax.js/parallax.min',
'slick': '/bower_components/slick-carousel/slick/slick.min',
'domReady': '/bower_components/requirejs/domReady',
},
shim: {
jquery: { exports: '$' },
angular: {
exports: "angular",
},
'angular-route': {
deps: ['angular'],
exports: 'angular'
},
'angularAMD': {
deps: ['angular'],
exports: 'angular'
},
'angular-slick': ['angular', 'slick'],
'angular-sanitize': {
deps: ['angular'],
exports: 'angular'
},
'materialize': ['jquery', 'jquery-hammer', 'hammerjs'],
'parallax': ['jquery'],
},
deps: ['app']
});
require(['app', 'other_controller', 'other_controller'], function(app) {
app.init();
});
Lastly make sure, you do not have ng-app in your html.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.