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'm using latest version of Require and Angular, but i encountered strange bug. I have controller for every view, but apparently works only for one view. This is my example code:

Define all controllers: Controllers.js

define([
   'modules/index/controller',
   'modules/test/controller'
], function(){});

Here works only with one controller, if i include 2 like here i get Error: ng:areq Bad Argument

Index: controller.js

define(['angular'], function(angular){

   'use strict';

   return angular.module('myApp.controllers', [])

   .controller('indexCtrl', ['$scope' , function ($scope){
        alert("index ok");
   }]);

});

Test: controller.js

define(['angular'], function(angular){

   'use strict';

   return angular.module('myApp.controllers', [])

   .controller('testCtrl', ['$scope' , function ($scope){
    alert("test ok");
    }])

});

Where i'm wrong?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

The problem is that you are creating the myApp.controllers module twice and one is overwriting the other. Assuming that you are manually bootstrapping angular after loading the controller, what you should be doing is to create the module first, then load that module to create controller:

app.js

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

Index: controller.js

define(['app'], function(app){
   'use strict';
   return app.controller('indexCtrl', ['$scope' , function ($scope){
        alert("index ok");
   }]);
});

Test: controller.js

define(['app'], function(app){
   'use strict';
   return app.controller('testCtrl', ['$scope' , function ($scope){
    alert("test ok");
    }])
});

I created angularAMD to facilitate use of RequreJS and AngularJS that might be of interest to you:

http://marcoslin.github.io/angularAMD/

share|improve this answer
    
That solved my problem. Thanks! –  user1202826 Feb 21 '14 at 18:07

Your Answer

 
discard

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