AngularJS best practices

Vladimir Lugovsky
Vladimir Lugovsky

Co-Founder

January 11, 2016

AngularJS best practices

A lot of times when I review Angular 1.x code, that my team writes, I keep seeing same mistakes. Those types of mistakes I made myself when I first faced it. It took time for me to realize some important concepts, which are not very clear from the first sight. Another thing is that in Javascript and especially in Angular it's possible to do some single thing in several different ways. Each one of them is good for some particular case, but there would often be one that covers most of your needs. So to eliminate the need of explaining this several times, I want to write an article to refer to. As well it would be great to share article with community as well so that they can point me to my mistakes.

Build

Build

Why did I put it first? Well, Angular has fairly easy setup, but as project grows proper build matters. And the most mature Angular 1.x build out there from my point of view is this Yeoman generator. So below I would just describe its main features.

Angular itself has module system. But it's a fact that it has nothing to do with loading Javascript into VM. Angular modules only specify how your application should be bootstrapped in a declarative way. That means that you need to insert all those <script> tags into HTML on your own. Initially, it doesn't look that bad, but in the end it gets annoying. You face another problem, when you go in production and realize that you need to concatenate and minify all your scripts.</script>
So you want so that all your application script files in development mode were injected as is via <script> tag, but in production mode you need to have that single script file, which contains all the code. The first problem that comes out here is that you need to have different index.html files for development and production mode. And you know the problem, when something should be modified, it should be modified in several places and so on.</script>
So what does this build does? It basically goes through your file system and finds all your javascript files in the app directory and injects them to the copy of index.html file created in temporary directory as <script> tags. If you are building your app in production mode, it will just add single <script> tag with minified code. It does the same with your bower dependencies. And how does it do it? Well if you look into index.html file you will notice following HTML comments.</script>

When build sees these types of comments it injects all your javascript files he's able to find in your file system as script tags. Magic!

Wise guys can say that there is another solution: you could use sourcemaps technique (this could be easily googled, don’t want to dig into explanations), but as from my experience it doesn’t work well. Sometimes when you jump to the line of code from chome dev console, it takes you to completely different file and line. As well it has some problems with watching local variables. Another thing is that doing concatenation every time takes fair amount of time, especially when you project is getting big.

There is several other cool things, that this build does, like ngInject usage, injection of bower dependencies, browsersync and many more. From my point of view, it would worth to review this build and just be aware of them.

Project structure

Project structure

When you will look through the articles on internet which tell you about angular project structure, they suggest two types:

  • Some suggest structuring your application by component type (classic Rails style): models/views/controllers/services etc.
  • Other suggest structuring the application by features. Basically I mean here structuring by modules or components. Of course, each module can have submodules.

NEVER GO THE FIRST WAY. It’s bad, it’s hard to support, it’s a mistake, especially for big application. As I first discovered that boilerplate I talked in previous paragraph, it was pretty weird for me, that they even put templates and css near js files. I came from java world, where everything was kept separately. Now it’s all good, because I realized how convenient it is. Today I’m completely okay to put even unit tests inside appropriate modules. This way, you can easily do refactoring, delete modules, which are not used, switch them temporary off etc. When you modify some directive, you don’t need to think where its template is located, how is it named, because it’s always near. Same for styles and unit tests. It’s really cool.

Performance

Agular is not slow

As I was learning Angular 1.x, a lot of times I either heard from colleagues or read on internet that Angular applications are slow. Right now I can't agree that. Angular is not slow.
When you're beginning to note, that your angular app is getting slow, first thing you need to think of is that you probably did something wrong. Well, Angular is very demanding in terms of performance. If you don't understand what's happening and write code just to make it work as expected, it will be slow for you. The reason why it can be so is because of Angular 1.x runs dirty checks in its $digest cycle practically every time you change something. And basically all performance optimizations are based on this technique. I will not point to all of them, because there's a nice article about performance optimizations .
Okay, you did all of these optimizations and your app is still slow. You should probably take a look at libraries you're using. Probably some library just creates too many "$watch"ers. If so, then think if you need everything this library provides. Probably you can just implement features you need in pure Javascript.
Okay, you checked any other library and it's still slow. Then maybe Angular is not the case? Maybe your application is just too large or you have some ineffective algorythm out there?

Usage of controller in templates

Well, it took me a while to understand, why do you need to use controller in templates rather than plain scope properties. Initially I didn't pay a lot of attention to this problem, but it became really important at some point, when application became large. In few words, you should always have dot inside of bindings of your templates. As usual there are a lot of articles out there describing this particular problem. I will just point to one of them.

Sharing data between controllers

Share data

And the last thing that seems pretty important for me is the question of sharing data between your controllers. At some point you will understand the need of it.
For example think about the simple application, which consists of two columns:
* A table of customers with filters * A map, which shows where your customers live. You want to make so, that when you change filters, the data should be changed in the table and inside the map. As well when you click some particular state on the map, filters should be changed as well and you should see in table items related only to some particular state.
It's obvious, that both table and map should have their own controllers. But here's the problem: they definitely share some data like filter values, filtered customer list etc. Where to put it? The most obvious answer is to put it to parent scope. But is this okay? I mean should parent know about the data, that it's children consume? Well, I think in this case no. If they will your controller in the end will become 10k+ line of code GOD controller. What about divide and conquer principle?
To eliminate this problem I suggest to put data of this kind to factories. This way you can simply inject that data into your controller. That easy! You will say: "No way! Those are singletones". Don't worry, so far I have never faced such problem when you need to create several instances of such model. Though it may happen, I don't think that this would be a big problem, that would be hard to resolve.
In any case as usual here are some nice articles about this problem.
As well you can read the following article and pay attention to first comment, if you have need of creation multiple instances of some factory which I described above.

Conclusion

At the time of writing I don't see any alternative of Angular, when you need to build web app fast. With all respect to React, it's not that mature for now. Angular comes with a lot of useful features out of the box which allows you to create your app really fast.
In this article I described the most important things junior/mid Angular 1.x developer should be aware of. If you thing I missed something, please put that in the comment section. I would be glad to discuss that.

Contact us
Akveo's case

Lead Generation Tool to Reduce Manual Work

Our client, Afore Capital, a venture capital firm focused on pre-seed investments, aimed to automate their lead generation processes but struggled with existing out-of-the-box solutions. To tackle this challenge, they sought assistance from our team of Akveo Retool experts.‍

The scope of work
The client needed a tailored solution to log and track inbound deals effectively. They required an application that could facilitate the addition, viewing, and editing of company and founder information, ensuring data integrity and preventing duplicates. Additionally, Afore Capital aimed to integrate external tools like PhantomBuster and LinkedIn to streamline data collection.

The result
By developing a custom Retool application, we streamlined the lead generation process, significantly reducing manual data entry. The application enabled employees to manage inbound deals efficiently while automated workflows for email parsing, notifications, and dynamic reporting enhanced operational efficiency. This allowed Afore Capital's team to focus more on building relationships with potential founders rather than on administrative tasks.

Learn more about the case

See More
See Less
Akveo's case

Retool CMS Application for EdTech Startup

Our client, CutTime, a leading fine arts education management platform, needed a scalable CMS application to improve vendor product management and user experience.

The scope of work
We developed a Retool application that allows vendors to easily upload and manage product listings, handle inventory, and set shipping options. The challenge was to integrate the app with the client’s system, enabling smooth authentication and product management for program directors.

The result
Our solution streamlined product management, reducing manual work for vendors, and significantly improving operational efficiency.

Learn more about the case

See More
See Less
Akveo's case

Building Reconciliation Tool for e-commerce company

Our client was in need of streamlining and simplifying its monthly accounting reconciliation process – preferably automatically. But with a lack of time and low budget for a custom build, development of a comprehensive software wasn’t in the picture. After going through the case and customer’s needs, we decided to implement Retool. And that was the right choice.

The scope of work

Our team developed a custom reconciliation tool designed specifically for the needs of high-volume transaction environments. It automated the processes and provided a comprehensive dashboard for monitoring discrepancies and anomalies in real-time.

The implementation of Retool significantly reduced manual effort, as well as fostered a more efficient and time-saving reconciliation process.

→ Learn more about the case

See More
See Less
Akveo's case

Creating Retool Mobile App for a Wine Seller

A leading spirits and wine seller in Europe required the development of an internal mobile app for private client managers and administrators. The project was supposed to be done in 1,5 months. Considering urgency and the scope of work, our developers decided to use Retool for swift and effective development.

The scope of work

Our developers built a mobile application tailored to the needs of the company's sales force: with a comprehensive overview of client interactions, facilitated order processing, and enabled access to sales history and performance metrics. It was user-friendly, with real-time updates, seamlessly integrated with existing customer databases. 

The result? Increase in productivity of the sales team and improved decision-making process. But most importantly, positive feedback from the customers themselves.

→ Learn more about the case

See More
See Less
Akveo's case

Developing PoC with Low Code for a Tour Operator

To efficiently gather, centralize, and manage data is a challenge for any tour operator. Our client was not an exception. The company was seeking to get an internal software that will source information from third-party APIs and automate the travel itinerary creation process. Preferably, cost- and user-friendly tool.

The scope of work

Our experts ensured the client that all the requirements could be covered by Retool. And just in 40 hours a new software was launched. The tool had a flexible and easy-to-use interface with user authentication and an access management system panel – all the company needed. At the end, Retool was considered the main tool to replace the existing system.

→ Learn more about the case

See More
See Less
Akveo's case

Testing New Generation of Lead Management Tool with Retool

Our client, a venture fund, had challenges with managing lead generation and client acquisition. As the company grew, it aimed to attract more clients and scale faster, as well as automate the processes to save time, improve efficiency and minimize human error. The idea was to craft an internal lead generation tool that will cover all the needs. We’ve agreed that Retool will be a perfect tool for this.

The scope of work

The project initially began as a proof of concept, but soon enough, with each new feature delivered, the company experienced increased engagement and value. 

We developed a web tool that integrates seamlessly with Phantombuster for data extraction and LinkedIn for social outreach. Now, the company has a platform that elevates the efficiency of their lead generation activities and provides deep insights into potential client bases.

→ Learn more about the case

See More
See Less
Akveo's case

Building an Advanced Admin Portal for Streamlined Operations

Confronted with the need for more sophisticated internal tools, an owner of IP Licensing marketplace turned to Retool to utilize its administrative functions. The primary goal was to construct an advanced admin portal that could support complex, multi-layered processes efficiently.

The scope of work

Our client needed help with updating filters and tables for its internal platform. In just 30 hours we've been able to update and create about 6 pages. Following features were introduced: add complex filtering and search, delete records, styling application with custom CSS. 

Together, we have increased performance on most heavy pages and fixed circular dependency issues.

→ Learn more about the case

See More
See Less
Akveo's case

Creating MVP Dashboard for Google Cloud Users

Facing the challenge of unoptimized cloud resource management, a technology firm working with Google Cloud users was looking for a solution to make its operations more efficient. The main idea of the project was to create an MVP for e-commerce shops to test some client hypotheses. Traditional cloud management tools fell short.

The scope of work

Determined to break through limitations, our team of developers turned Retool. We decided to craft an MVP Dashboard specifically for Google Cloud users. This wasn't just about bringing data into view; but about reshaping how teams interact with their cloud environment.

We designed a dashboard that turned complex cloud data into a clear, strategic asset  thanks to comprehensive analytics, tailored metrics, and an intuitive interface, that Retool provides. As the results, an increase in operational efficiency, significant improvement in cost management and resource optimization.

→ Learn more about the case

See More
See Less
Akveo's case

Elevating CRM with Custom HubSpot Sales Dashboard

Our other client, a SaaS startup, that offers collaborative tools for design and engineering teams, was on a quest to supercharge their sales efforts. Traditional CRM systems were limited and not customizable enough. The company sought a solution that could tailor HubSpot to their workflow and analytics needs.

The scope of work

Charged with the task of going beyond standard CRM functions, our team turned to Retool. We wanted to redefine how sales teams interact with their CRM. 

By integrating advanced analytics, custom metrics, and a user-friendly interface, our developers provided a solution that transformed data into a strategic asset.

In 40 hours, three informative dashboards were developed, containing the most sensitive data related to sales activities. These dashboards enable our customer to analyze sales and lead generation performance from a different perspective and establish the appropriate KPIs.

→ Learn more about the case

See More
See Less
Akveo's case

Retool for Sales and CRM Integration

See More
See Less
Akveo's case

Building a PDF Editor with Low-Code

Our client, a leading digital credential IT startup, needed a lot of internal processes to be optimized. But the experience with low-code tools wasn’t sufficient. That’s why the company decided to hire professionals. And our team of developers joined the project.

The scope of work

The client has a program that designs and prints custom badges for customers. The badges need to be “mail-merged” with a person’s info and turned into a PDF to print. But what is the best way to do it?

Our developers decided to use Retool as a core tool. Using custom components and JavaScript, we developed a program that reduced employees' time for designing, putting the data, verifying, and printing PDF badges in one application.

As a result, the new approach significantly reduces the time required by the internal team to organize all the necessary staff for the conference, including badge creation.

→ Learn more about the case

See More
See Less
Subscription
Subscribe via Email

Want to know which websites saw the most traffic growth in your industry? Not sure why your SEO strategy doesn’t work?

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

By clicking “Subscribe” you agree to Akveo Privacy Policy and consent to Akveo using your contact data for newsletter purposes

More articles by themes

Cross
Contact us
AnnaRodionEvgenyExpertExpertExpert
Cross
Got any questions?
Our domain expert is here to answer
If you have any questions, feel free to leave me a personal message on LinkedIn. We are here to help.
Thanks for your question
We will contact you soon
We have a problem
Please, check the entered data
Got any questions?