I decided to use RequireJS with AngularJS.
<script data-main="/app.js" src="/bower_components/requirejs/require.js"></script>
<div ng-app>
Hello {{"angular-require_test"}} world!
</div>
This works. But strangely, if I try to name my app (ng-app="app"
) it breaks
<script data-main="/app.js" src="/bower_components/requirejs/require.js"></script>
<div ng-app="app">
Hello {{"angular-require_test"}} world!
</div>
It throws the error
Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/1.2.23/$injector/modulerr?p0=app&p1=Error%3A%20…90347)%0A%20%20%20%20at%20X%20(http%3A%2F%2Flocalhost%2Fapp.js%3A9%3A90557) app.js:9
Example hosted at runnable. It's the version that works. Add ng-app="app"
to the index.html
to break it : http://runnable.com/U_46hUAJdhQwnzSP/angular-require_test-for-node-js
Here's the rest of my code/setup:
server.js:
var express = require('express');
var app = express();
app.use(express.static('.'));
app.use('bower_components', express.static('bower_components'));
var server = require('http').createServer(app);
server.listen(80, function() {
console.info('Express server started');
});
Gruntfile.js:
module.exports = function grunt(grunt) {
require('load-grunt-tasks')(grunt);
grunt.initConfig({
requirejs: {
compile: {
options: {
baseUrl: '.',
mainConfigFile: 'main.js',
paths: {
jquery: 'bower_components/jquery/dist/jquery.min',
angular: 'bower_components/angular/angular.min'
},
shim: {
'angular': {
exports: 'angular',
},
},
name: 'main',
out: 'app.js',
}
}
},
});
grunt.registerTask('default', ['requirejs']);
};
main.js
require(['jquery', 'angular'], function($, angular){
$(document).ready(function(){
alert('jquery loaded, document ready');
});
});