AngularJS best practices

Posted By : Ankit Gupta | 11-Jun-2014

I am going to write  some angular-js best practices:


1. Divide your app into multiple modules :   Depending on size and complexity of the project,number of modules should be decided,try to write a separate module for individual business model.In this way, we can have some kind of modularity in the code.

2. Directory Structure :   A proper directory structure should be developed for each module like separate folders for controllers,services,directives etc.
Example :

  • module 1:
  •     controllers
  •         myController1.js
  •     services
  •         myService1.js
  •     directives
  •         myDirective1.js
  • module 2:
  •     controllers
  •         myController2.js
  •     services
  •         myService2.js
  •     directives
  •         myDirective2.js


3. Naming conventions :   Naming conventions should be very good and should follow a fixed pattern,like if we are writing a controller for handling employ module than name can be like employController.js.Apart from pattern,naming should be done in a way where it becomes easy to identify pre-defined components and user-defined components.

4. Avoid scripting inside html :   We should not use scripting inside html while using angular js,its a major feature of angular.Instead of writing scripts inside html ,angular directives should be used or we can also define our custom directive where script can be put and html remains very clean.

5. Use angular script :   Although angular supports jquery and other types of script APIs,but use of angular script API is preferred to avoid version conflicting problems of other APIs.

6. Use of directives :   Angular directives is a very cool feature that enables us to write once and use any where.We should try to develop more directives as they can be used in multiple html templates.

7. Use of filters :   Angular provides some filters which can be used for effective output,try to write custom filters for business requirements.

8. Keep controllers simple :   Controllers should only contain logic regarding scope objects definitions and view rendering.It should not contain DOM interaction or DOM manipulation logic.

9. Separate layer for server interaction :   Try to write separate layers for server interaction and it should not be mixed with data processing and client side business logic.In this way, code will be easy to maintain.

10. Use latest features :   Angular is growing fast and providing many tools,so try to use latest tools for application.

 

 

 

About Author

Author Image
Ankit Gupta

Ankit is a lead java developer well experienced in development of web and enterprise level applications. He has good exposure to enterprise frameworks like Drools , Opta-planner , jBilling and graph databases like Neo4J etc.

Request for Proposal

Name is required

Comment is required

Sending message..