Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Im trying to add AngularJS to my web application which already makes use of RequireJS. When loading my test page, i am seeing:

1) Error: No module: MyApp
      ...),factory:a("$provide","factory"),service:a("$provide","service"),value:a("$prov...

2) TypeError: Modernizr is undefined
if (!Modernizr.history) {

I am using AngularJS v1.1.5

Here's my tree:

resources
   - CSS
   - js
        - controllers
             - MainController.js
        - libs
             - angular.js
             - jquery.js
             - require.js
             - mondernizr.js
             ......
             ......
             ......
        main.js
        mainApp.js
pages
        test.html

main.js

(function(require) {
'use strict';

require.config({
    baseUrl: '/resources/js',
    paths: {
        'zepto'     : 'libs/zepto',
        'jquery'    : 'libs/jquery',
        'angular'   : 'libs/angular',
        'router'    : 'libs/page',
        'history'   : 'libs/history.iegte8',
        'event'     : 'libs/eventemitter2'
    },
    shim: {
        'zepto'     : { exports: '$' },
        'angular'   : { exports : 'angular' },
        'router'    : { exports: 'page'},
        'modernizr' : { exports: 'Modernizr' }
    }
});

require([ 'jquery', 'angular', 'routes', 'modernizr', 'event' ], function($, angular, routes, Modernizr, Event) {
    function bootstrap() {
        var app = new Event(),
            router = routes(app);

        if (typeof console !== 'undefined') console.info('>> module routes loaded ... executing router');
        router({ click: false, popstate: false });
        if (typeof console !== 'undefined') console.info('>> executed router');
    }

    $(function() {
        if (!Modernizr.history) {
            require([ 'history' ], bootstrap);
            require([ 'controllers/MainController' ], bootstrap);
        } else {
            require([ 'controllers/MainController' ], bootstrap);
            bootstrap();
        }
    });
});


})(this.require);

mainApp.js

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

MainController.js

require(['mainApp'], function(mainApp) {
mainApp.controller('MainController', function($scope) {
    $scope.data = {message: "Hello"};
})
});

test.html

<!DOCTYPE html>
<html ng-app="MyApp">
<head>
<script src="/assets/js/vendor/require.js" data-main="/assets/js/main"></script>
</head>
<body>
       <div ng-controller="MainController">
    {{ data.message + " world" }}
       </div>   

</body>
</html>

Any help appreciated.

share|improve this question

1 Answer 1

The No module: MyApp problem is causes by the Angular automatic initialization: <html ng-app="MyApp">. Angular loads before mainApp.js, sees the ng-app and tries to find a module which is not (yet) there.

The solution is to manually bootstrap Angular from within main.js and inside the document load event, as described here.

I am not sure about the problem with Modernizr; I guess you are NOT loading it at all. RequireJs is not complaining because of the shim. How are you loading it? It is not included in your paths configuration. You may want to load Modernizr as independent script before all other scripts (as per recommendations), in which case no paths configuration is needed and the shim is enough.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.