0

I'm a bit new to Angularjs. I'm confused about multiple controllers. I know Angular app can have multiple controllers. But I'm confused when to use multiple controllers. What's the advantage of having multiple controllers? Can anyone help me to clarify this? Thanks

2 Answers 2

1

In order to modularize your application based on the feature wise, you can have multiple controllers.

For example if you have a login feature, you can have a separate controller which does the login part(Fetching data,checking the authentication etc)

var app = angular.module('app', []);

app.controller('LoginController', function ($scope) {
    //Controller Code Here which fetches the API and check authentication
});

app.controller('ProductController', function ($scope) {
    //Controller Code Here which loads the products
});
Sign up to request clarification or add additional context in comments.

Comments

0

For different functions we need different controllers. For example if you need a ui modal to display something then for that modal you need different controller which will handle only modal's functionality. If you try to code everything in single controller then it will be confusing for you when in future you want to edit some contents of any html page.

Comments

Your Answer

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