0

I'm trying for the first time to use AngularJS in conjunction with RequireJS using this guide as a basis. As far I can tell after a lot of debugging I'm loading all my modules in the correct order, but when the application runs Angular throws an Error / Exception with the following message:

Argument 'fn' is not a function, got string from myApp

I've seen this message before due to syntax errors, so even though I've looked trough the code multiple times I won't rule out the possibility of a simple syntax error. Not making a Fiddle just yet in case it is something as simple as a syntax error, but I'll of course do so if requested.

Update: I just noticed when setting ng-app="myApp" in the <html> tag I also get an additional error,

No module: myApp

Update II: Okay, it turns out it indeed was an syntax error in the only file not included below. I am though still left with the problem from update I.

RequireJS bootstrap

'use strict';
define([
    'require',
    'angular',
    'app/myApp/app',
    'app/myApp/routes'

], function(require, ng) {
    require(['domReady'], function(domReady) {
        ng.bootstrap(domReady, ['myApp']);
    });
});

app.js

'use strict';
define([
    'angular',
    './controllers/index'
], function(ng) {
    return ng.module('myApp', [
         'myApp.controllers'
    ]);
    }
);

controllers/index

'use strict';
define([
    './front-page-ctrl'
], function() {

});

controllers/module

'use strict';
define(['angular'], function (ng) {
    return ng.module('myApp.controllers', []);
});

controllers/front-page-ctrl

'use strict';
define(['./module'], function(controllers) {
    controllers.
        controller('FrontPageCtrl', ['$scope', 
             function($scope) {
                console.log('I\'m alive!');
            }
        ]);
});
2
  • You shouldn't change your question once it's posted, because existing answers are left without context. Regarding your new "No module" problem that's because you're using ng-app when you shouldn't. ng.bootstrap serves the exact same purpose as ng-app. For more info see stackoverflow.com/a/17498509/1095616. Commented Nov 1, 2013 at 11:34
  • Yeh, sorry about that. I realized the bootstrapping part just after I updated it. Will mark your answer as accepted as you pointed me in the right direction here. Commented Nov 1, 2013 at 12:33

2 Answers 2

1

Delete ng-app="myApp" from your html. Because it has bootstrapped manually ng.bootstrap(domReady, ['myApp']);

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

Comments

0

RequireJS docs on Dom ready state:

Since DOM ready is a common application need, ideally the nested functions in the API above could be avoided. The domReady module also implements the Loader Plugin API, so you can use the loader plugin syntax (notice the ! in the domReady dependency) to force the require() callback function to wait for the DOM to be ready before executing. domReady will return the current document when used as a loader plugin:

So, when you require 'domReady' the result is a function:

function domReady(callback) {
    if (isPageLoaded) {
        callback(doc);
    } else {
        readyCalls.push(callback);
    }
    return domReady;
} 

But when you append the domReady string with ! sign the result will be the actual document element:

'use strict';
define([
    'require',
    'angular',
    'app/myApp/app',
    'app/myApp/routes'

], function(require, ng) {
    require(['domReady!'], function(domReady) {
        // domReady is now a document element
        ng.bootstrap(domReady, ['myApp']);
    });
});

1 Comment

Thank you for your reply. I was partly aware of what you are saying, but I guess I did judgement call when using domReady without the trailing exclamation mark due to it not being there in the guide. Your answer does however make me certain it should be there. However, this didn't solve the problem I'm afraid.

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.