0

I am using AngularJS with RequireJS.

NEW QUESTION: RequireJS does not load any controllers. I am using this tutorial.

OLD QUESTION: RequireJS loads the main.js file, the bootstrap.min.js file and jQuery. This works good. But it does not load angular for some reason.

I am using chrome, and in the developer tools tab "network" there are only the 3 files mentioned above loaded.

console.log(typeof angular); equals to undefined. ( I have tried that in the developer console )

There is no error message shown in the console!

SOLUTION: I fixed it by adding 'angular' to the deps array.

My main.js code is: // Require JS Configuration

require.config({ // Code format changed
// alias library paths
paths: {
    'jquery': '../bower_components/jquery/dist/jquery.min',
    'domReady': '../bower_components/requirejs-domready/domReady',
    'angular': '../bower_components/angular/angular.min',
    'angular-route': '../bower_components/angular-route/angular-route.min',
    'ngFx': '../bower_components/ngFx/dist/ngFx.min',
    'angular-ui-router': '../bower_components/angular-ui-router/release/angular-ui-router.min',
    'bootstrap': '../bower_components/bootstrap/dist/js/bootstrap.min'
},

// angular does not support AMD out of the box, put it in a shim
shim: {
    'angular': {
        exports: 'angular'
    },
    'angular-route': {
        deps: [ "angular" ]
    },
    'ngFx': {
        deps: [ "angular" ]
    },
    'angular-ui-router': {
        deps: [ "angular" ]
    },
    'bootstrap': {
        deps: [ "jquery" ]
    }
},

// Kick start application
deps: [
    './bootstrap',
    'domReady',
    'angular',
    'angular-route',
    'ngFx',
    'angular-ui-router',
    'bootstrap'
]
});

My bootstrap.js file:

define([
   'require',
   'angular',
   'app',
   'routes'
], function (require, ng) {
    'use strict';

    require(['domReady!'], function (document) {
        ng.bootstrap(document, ['app']);
    });
});

My app.js file:

define([
    'angular',
    './controllers/index',
    './directives/index',
    './filters/index',
    './services/index'
], function (ng) {
    'use strict';

    return ng.module('app', [
        'app.services',
        'app.controllers',
        'app.filters',
        'app.directives',
        'ngFx',
        'ngRoute'
    ]).config(function ($urlRouterProvider) {
        $urlRouterProvider.otherwise('/');
    });
});

Folder structure:

angular
    controllers
        index.js
        menu.js
        module.js
    directives
    filters
    services

    app.js
    bootstrap.js
    main.js
    routes.js
bower_components
    ...

Thank you for your help! :-)

2
  • 2
    add 'angular' into deps array in after './bootstrap' Commented Oct 10, 2014 at 7:12
  • 1
    You're unnecessarily declaring multiple exports properties for Angular. The only one you should need is for 'angular', get rid of the rest. Commented Oct 10, 2014 at 7:43

3 Answers 3

1

I think you should use angular.bootstrap with RequireJS. Also you should add angular to deps. Here api reference:

https://docs.angularjs.org/api/ng/function/angular.bootstrap

Sign up to request clarification or add additional context in comments.

1 Comment

Okay, I have setup this using this Tutorial - Adding Angular to the deps worked fine. Now there is the problem, that the controllers are not beeing loaded from the bootstrap.
0

The following fixed my problems:

  1. Add the dependencies to the main.js's deps array.
  2. Twitter Bootstrap and the Bootstrap.js file were running into a name conflict. I solved this issue by renaming the 'bootstrap': '../bower_components/bootstrap/dist/js/bootstrap.min' to 'tw-bootstrap': '../bower_components/bootstrap/dist/js/bootstrap.min' and renamed all other relations.

Comments

0

Looking for the solution for same issue last couple of hours, dig out every possible way but finally got the solution by Renaming angular module name 'app' to something else i.e 'myApp'. then it start working.

Amended code here

define([
   'require',
   'angular',
   'app',
   'routes'
], function (require, ng) {
    'use strict';

    require(['domReady!'], function (document) {
        ng.bootstrap(document, ['myApp']);
    });
});
My app.js file:

define([
    'angular',
    './controllers/index',
    './directives/index',
    './filters/index',
    './services/index'
], function (ng) {
    'use strict';

    return ng.module('myApp', [
        'app.services',
        'app.controllers',
        'app.filters',
        'app.directives',
        'ngFx',
        'ngRoute'
    ]).config(function ($urlRouterProvider) {
        $urlRouterProvider.otherwise('/');
    });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.