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.

I am new to requireJS, and I wanted to get a simple starter 'hello worldish' project up and running. I'm missing something though, because I get angular is not defined as a JS error when the GreetCtrl tries to load.

index.html:

<!DOCTYPE html>
<html ng-app="ReqApp" ng-controller="GreetCtrl">
  <body>
    <h1>{{greeting}}!</h1>
    <script src="assets/require/require.js" data-main="assets/require/main"></script>
  </body>
</html>

main.js:

require.config({
    // alias libraries paths
  paths: {
    'domReady':      'domReady',
    'angular':       '../../vendor/angular/angular.min',
    'GreetCtrl':     '../../src/app/modules/GreetCtrl',
    'app':           '../../src/app/app'
  },
  // angular does not support AMD out of the box, put it in a shim
  shim: {
    'angular': {
      exports: 'angular'
    }
  },
  // kick start application
  deps: ['./bootstrap']
});

bootstrap.js:

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

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

app.js:

define([
  'angular',
  'GreetCtrl'
], function (ng) {
  'use strict';

  return ng.module('ReqApp', [
    'ReqApp.GreetCtrl'
  ]);
});

and finally, GreetCtrl.js:

angular.module( 'ReqApp.GreetCtrl', [])
.controller( 'GreetCtrl', function GreetCtrl ($scope) {
  $scope.greeting = "greetings puny human";
});

According to firebug, line 1 of GreetCtrl.js throws an error of angular is not defined. What am I missing here?

Thanks in advance!!

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

You have not told the GreetCtrl that it depends upon angular thus it is undefined. Try changing the GreetCtrl to be like this

define(['angular'], function(angular){
    angular.module( 'ReqApp.GreetCtrl', [])
    .controller( 'GreetCtrl', function GreetCtrl ($scope) {
         $scope.greeting = "greetings puny human";
    });
});
share|improve this answer
 
Ug how did I miss that?? Thanks so much for taking the time to help me!!! –  tengen Oct 24 '13 at 20:39
add comment

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.