Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular.js with require.js getting Uncaught Error: [$injector:modulerr]

Tags:

angularjs

I am trying to use require.js with Main.js and getting following error:

Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/1.2.0rc1/$injector/modulerr?p0=MyApp&p1=Error%3…3A8080%2FResults%2Fresources%2Fjs%2Fvendor%2Fangular.min.js%3A31%3A252)

My Main.js

 require.config({

    // alias libraries paths
    paths: {
        'angular': 'vendor/angular.min',
        'jquery': 'vendor/jquery-1.8.0-min',
        'angularroute': 'vendor/angular-route'          
    },

    // angular does not support AMD out of the box, put it in a shim
    shim: {
        'angular': {
            deps: ['jquery'],
            exports: 'angular'
        },
        'angularroute':{
            deps:['angular']
        }

    }
});
require([
         'angular',
         'angularroute',
         'app'
         ],
        function(angular, angularroute, app){
    'use strict';       
})

My app.js

define(['angular'], function(angular){
    return angular.module('MyApp',['ngRoute']);
});

I got an error that said I need to add angularroute to my app but I still get this error. Can anyone point me to what i might be doing wrong?

like image 508
user1647708 Avatar asked Oct 10 '13 22:10

user1647708


3 Answers

I found a solution here: https://github.com/tnajdek/angular-requirejs-seed/blob/master/app/js/main.js

from the docs: http://code.angularjs.org/1.2.1/docs/guide/bootstrap#overview_deferred-bootstrap

window.name = "NG_DEFER_BOOTSTRAP!";
require(['angular', 'angularroute', 'app'], function(angular, angularroute, app){
    'use strict';

    //after you are done defining / augmenting 'MyApp' run this:

    angular.element().ready(function() {
        angular.resumeBootstrap([app['name']]);
    });
})
like image 77
Oberoth Avatar answered Nov 01 '22 14:11

Oberoth


Here is my require config, at require_config.js:

var require = {
    baseUrl: '/js',
    paths: {
        'angular': 'lib/angular.min',
        'angular.route': 'lib/angular-route.min',
        'angular.resource': 'lib/angular-resource.min',
        'angular.animate': 'lib/angular-animate.min',
        'angular.ui.bootstrap': 'lib/ui-bootstrap-tpls-0.6.0.min',
        'angular.upload': 'lib/ng-upload.min',
        'jquery': 'lib/jquery-1.10.2.min'
    },
    shim: {
        'angular': ['jquery'],
        'angular.route': ['angular'],
        'angular.resource': ['angular'],
        'angular.animate': ['angular'],
        'angular.ui.bootstrap': ['angular'],
        'angular.upload': ['angular'],
        'my.angular': ['angular', 'angular.route', 'angular.resource', 'angular.animate', 'angular.ui.bootstrap', 'angular.upload']
    }
};

My script tags at HTML <head>:

<script src="/js/require-config.js"></script>
<script src="/js/lib/require.js"></script>

The pages that come from server-side MAY make usage of Angular, or jQuery, both, or none... so, the HTML markup may contain a single simple tag to eventually activate the whole Angular structure, like this:

<script>require(['myApp']);</script>

Now, at myApp.js, I just depend on my.angular, which takes care of loading everything else... in fact, I have other flavours of "my.angular", with different subsets, which I use in different contexts of the site:

define(['my.angular'], function() {
    var myApp = angular.module('myApp', ['ngRoute', 'ngResource', 'ngAnimate', 'ngUpload', 'ui.bootstrap']);
   // more app module stuff here, including bootstrap
    return myApp;
});
like image 32
J. Bruni Avatar answered Nov 01 '22 15:11

J. Bruni


angular is defined as a global variable and your declaration is overriding it. The file angular.js does not return anything so there is nothing to inject at the RequireJS level. Try the following:

define(['angular'], function(){
    return angular.module('MyApp',['ngRoute']);
});

I was struggling with RequireJS and AngularJS so I created angularAMD to help me:
http://marcoslin.github.io/angularAMD/

like image 3
marcoseu Avatar answered Nov 01 '22 15:11

marcoseu