Arte,
You have two options imho:
a) Pass 'Controller3' as a Function and not as a string and make it
available like AppController
b) Use the DI goodness of angular and instantiate the function through
its mechanism:
You need to:
define([], function() {
angular.module('myApp').controller('Controller3',
['$scope', function Controller3 ($scope) {
$scope.test = 'OK';
}];
);
end;
angular.module('myApp') locates the controller.
controller is a helper function to register a controller by a name.
This way when angular needs it'll be able to ask DI mechanism by the
name.
As discussed previously in this forum, requirejs and DI mechanism can
coexist.
Each one exists serves a different purpose.
With requirejs you can modularize, reach the code and minificate.
DI lets you make the pieces more pluggable, testable.
In fact I like option b) more. Problem is that I was not able to make
it work sucessfully.
I'm experiencing the following problem:
I have a main.js like this:
define(["jquery",
"angular",
"conf",
"controllers/controllers",
"controllers/UsuariosCtrl",
"jquery.alpha",
"jquery.beta",
"filters/filters",
"directives/directives",
"services/services"
], function($, angular, conf, ctrl, UsuariosCtrl) {
$(function() {
$('body').alpha().beta();
// Declare app level module which depends on filters, and
services
var mod = window.angular.module(conf.moduloRaiz, [conf.services,
conf.filters, conf.directives, conf.controllers]).
config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/view1', {template: 'partials/
partial1.html', controller: 'MyCtrl1'});
$routeProvider.when('/view2', {template: 'partials/
partial2.html', controller: 'MyCtrl2'});
$routeProvider.when('/usuarios', {template: 'partials/
usuarios.html', controller: UsuariosCtrl});
$routeProvider.otherwise({redirectTo: '/'});
}]);
angular.bootstrap(window.document,[conf.moduloRaiz,
conf.services, conf.filters, conf.directives, conf.controllers]);
});
});
a controllers/controllers.js like this:
define(["angular", "conf", "require"], function( angular, conf,
require) {
angular.module(conf.controllers, []);
require(["controllers/UsuariosCtrl",
"controllers/MyCtrl1",
"controllers/MyCtrl2",
"controllers/MainCtrl"]);
});
a controllers/MyCtrl1.js like this:
define(["angular","conf"], function(angular, conf){
angular.module(conf.controllers).controller('MyCtrl1', ['$scope',
'$window', function($scope, $window) {
$scope.btnTeste = function() {
$window.alert('clicou');
}
}]);
});
When I load the page I get:
Error: Argument 'MyCtrl1' is not a function, got undefined
My first thought was that was an angular problem, but if make the
MyCtrl1 declaration in main.js file it works.
My second thought was scope, and I've suspected the angular.js module
I've made:
define(['./js/lib/angular/angular.js'], function(){
if (typeof _ != 'undefined') {
_.noConflict();
}
if(typeof $ != 'undefined') {
$.noConflict() ;
}
return window.angular;
});
It just load angular file, and returns window.angular, and that's
where my head twists angular is a global object reachable through
window.
If i make a watch expression window.angular === angular on chrome it
says it's true.
requirejs here is just loading one angular file. Later I can put
another ones like angular-resource.js etc.
I hope this helps you somehow, and if some kind soul can help me I
would be very grateful, because I spent one entire week figuring this
out.
(Of course I'm learning about this amazing framework in the process).
Thanks in advance,
Geraldo Lopes de Souza