I would like to define my controller using the module pattern like this:
(function(ng, app) {
// Controller constructor.
function Controller($scope) {
this.scope = $scope;
this.scope.fmembers = [
{
name: 'Member 1',
role: 'Head of Family',
age: 55
},
{
name: 'Member 2',
role: 'Brother of Head of Family',
age: 51
}
];
Controller.prototype = {
getFMembers: function() {
return this.scope;
}
};
return( this );
}
// Define the Controller as the constructor function.
app.controller('ftreeController', Controller );
})(angular, anmDesktop);
If I do that how can I retrieve this controller (ftreeController) from the module? For example, I would like to use freeController in the routeprovider in app.config:
$routeProvider.when('/view2', {
templateUrl: 'partials/partial2.html',
controller: ftreeController'
});
In the routeprovider described above, I get an error (ftreeController not defined ...)
Thank you.
There were a few errors in your code (like missing quotes) that I assume are copy and paste errors when making the post. Correcting those, it's just a matter of loading order.
The controller must be defined before the routes are configured and your app must be defined before both. I also put routes in a similarly-defined file to show how this could work on scale.
However, absent a loading system like AMD, I am not sure why you are choosing to define your code this way, but it will work.
I created a Plunker to demonstrate it working.
A few things to keep in mind: first, controllers in Angular are just functions. I am not sure why you created a copy of the scope and added it as a member property of the controller object nor why you declared a controller function off the prototype. Because of Angular's dependency injection, the function will have everything it needs when it runs, so we can just use a normal, plain-old javascript function. In most angular projects, controllers are just defined like this:
app.controller( 'myCtrl', function( $scope ) {
//...
});