Blog

Angular 1: Properly Using Controllers

Angular 1 was a game changer for front-end development, thanks to its strong emphasis on code organization through the MVC pattern. This means, thanks to dependency injection, your client-side code stays clean and structured. The use of controllers, services, and modules in Angular 1 highlights the separation of concerns that keeps your application maintainable.

But even with these strengths, Angular 1 applications can become unmanageable if developers misplace logic. This is particularly true for controllers. If you're used to traditional MVC frameworks, you might think controllers are the place for all your application's logic. However, applying the same pattern to Angular 1 can lead to poor practices.

Key Takeaways

  • Angular controllers should act as a bridge between the view and business logic, not the place for business logic itself.
  • Reuse logic through services to keep controllers clean and maintainable.
  • Direct DOM manipulation should be handled by directives, not in controllers.

What Not To Do With Angular Controllers

The primary role of a controller should be to bind data to your view, not to execute business logic or manage application state. For instance, it’s a common mistake to make an HTTP request directly in a controller using the $http service. While you get a neat binding for your view, it locks that data in that single controller, making it hard to reuse elsewhere.

Use Services to Abstract Shared Functionality

Services offer a better way to manage shared functionality across your app. They are singletons, meaning their state and methods can be reused across the app without duplication. This lets you make a single HTTP request in a service and use that data in multiple controllers without repeated code or extra HTTP requests, keeping your app DRY.

Save DOM Manipulation For Directives

Keep your controllers free from direct DOM manipulation. While it's tempting to throw in some quick jQuery, it won't scale. As your application grows, the clutter of blending view logic with data logic can lead to a tangled mess. Use directives for any DOM changes. They leverage the strong angular binding system and keep your logic coherent and organized.

Conclusion

Designing Angular 1 applications requires an understanding of its specific conventions and strengths. Controllers shouldn't be overburdened with logic that belongs in services or directives. Recognize the role each component plays — controllers for data binding, services for shared logic, and directives for DOM manipulation — and you'll harness the full potential of Angular 1's architecture.

FAQ

Why shouldn't I use $http in my controller?

Using $http in a controller ties the data retrieval to that specific controller. This limits reusability and makes it difficult to share data across your application.

What's the benefit of using a service over a controller?

Services offer a way to encapsulate shared logic that can easily be injected wherever needed, reducing duplication and enhancing maintainability.

Can I use jQuery in my Angular app?

Technically, yes. However, using jQuery directly in controllers can lead to tangled code. It's better to handle DOM manipulations in directives to keep a clean separation of concerns.

Is Angular 1 still relevant in 2026?

While newer versions of Angular offer more features and benefits, Angular 1 is still used in legacy systems. Many principles discussed remain valid when using modern Angular frameworks.

Mastering the tech interviewWhat everyone is doing wrong in tech interviews