0

I have many controllers in app and I don't want to load them upfront as the combined JS becomes very heavy.

So I have tried following:

In my main app.js, I have route defined as below:

$routeProvider.when('/myaccount', {
    templateUrl: 'my-account.html',
    title: 'My Account'
})

The my-account-controller.js has controller defined like:

angular.module('myApp',[]).controller('my-account', ['$scope', function($scope) { 
    $scope.greeting = 'Hola!';
}]);

And finally in my-account.html, I have a script tag which should load the controller syncronously before using it. I am attaching that contorller with ng-controller directive:

script src='/assets/my-account-controller.js'
<div class="container" ng-controller='my-account'>
   ...
</div>

The problem is somehow, Angular can not identify my-account as controller and it throws following error:

Error: [ng:areq] Argument 'my-account' is not a function, got undefined

Can someone tell me what is the missing piece?

1
  • did you find a way to do so though? I also have a same problem Commented Dec 25, 2016 at 9:08

2 Answers 2

1

You should use UpperCamelCase in your controller name, and i don't know if you already have defined myApp, if yes, you should remove the second argument from the module method since it will reset the dependencies:

so this -> angular.module('myApp',[]) should be -> angular.module('myApp')

angular.module('myApp').controller('MyAccount', ['$scope', function($scope) { 
    $scope.greeting = 'Hola!';
}]);

And in your view:

<script src='/assets/my-account-controller.js'
<div class="container" ng-controller='MyAccount'>
   ...
</div>

And the route:

$routeProvider.when('/myaccount', {
    templateUrl: 'my-account.html',
    title: 'My Account',
    controller: 'MyAccount'
});

You can read more about angular best practices here

Sign up to request clarification or add additional context in comments.

4 Comments

@MitulShah check my edit regarding the dependencies, I believe you should not be passing the second argument there.
The only problem here is I don't want to specify the controller inside route file as there are many controllers. So I am using ng-controller directive for that in HTML, after loading the my-account-controller.js with script tag, but somehow Angular is still not able to identify that as controller. Maybe we need it specified before hand?
did you try removing the second parameter from the module method? the empty array. try removing it please.
Yes, I removed them. I think the problem here is: I have a script tag inside the template, which is not executed. And so effectively my-account-controller.js is never executed.
0

This is not the proper way to do it. You have to declare your controller in the route you are using such as

$routeProvider.when(''/myaccount'', {
              templateUrl: 'my-account.html',
              controller: 'my-account',
              title: 'My Account'
})

3 Comments

Yes, but in that case I have to load my-account (plus all other) controller in that file, which is something I don't want.
I don't think so. If for each view you have one controller this is the best way to do it. Moreover, the proper way to name a controller (according to AngularJs documentation) would be MyAccountController in your case. You should start with that.
Yes, I tried to specify MyAccount as controller without loading that in this file which throws an error. I have already renamed the controller.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.