The 10 best Angular tips selected by the community
10 tips and tricks from June every day challenge that have the most likes and explanation of their concepts in more detail.

The 10 best Angular tips selected by the community
10 tips and tricks from June every day challenge that have the most likes and explanation of their concepts in more detail.


Waterplea and I took an interesting challenge this June: we wrote an Angular tip every day on Twitter. It was warmly welcomed by the Angular community.
I decided to write an article about 10 tips and tricks that have the most likes and explain their concepts in more detail.
Let’s start!
Tokenize global objectsLink to this section
The most popular tweet was about DI tokens of global objects.
In frontend, we are used to many global objects available in any scope. We use objects like window
, document
, fetch
method, location
, etc. We don't expect that there can be situations when we do not have them.
But for example, there is no browser, no Window and no DOM in Angular Universal or Jest testing environment. And having global objects as tokens, you can use, replace and test code without problems.
?#AngularTip for the day! You can tokenize global objects to use them with #angular Dependency Injection. It allows you to make your entities more abstract and easy to replace and test
— Roman Sedov (@marsibarsi) June 21, 2020
By the way, we have all the basic tokens in our library: https://t.co/zPMj3orUpT pic.twitter.com/D3S6qCrvyE
If you want to know more about tokens and deepen your knowledge about Angular Dependency Injection mechanism, check my free chapter about DI on angular.institute
Extend Observable or SubjectLink to this section
I saw many developers who created good services in their apps that make one thing well. Because we use RxJS, service can contain one Observable
or Subject
inside and do some data transformation for it.
So, you can simplify such situations extending your services from Observable or Subject:
?#AngularTip for the day! Do not forget that your #angular services can extend Observable. It is just a class ?
— Roman Sedov (@marsibarsi) June 20, 2020
Read more about how we use it in Web APIs for Angular here: https://t.co/PZyXR5fk5I
And take a look at this sample of GeolocationService implementation pic.twitter.com/sfLX5kSUF9
Control value as ReplaySubjectLink to this section
There can be situations when you need to subscribe to control valueChanges
and get its current value as well. So, do not reinvent the wheel, just you the following wrapper:
?#AngularTip for the day! You can turn your #angular AbstractControl / AbstractControlDirective valueChanges into ReplaySubject(1) with a simple wrapper
— Roman Sedov (@marsibarsi) June 6, 2020
?Gist to copy: https://t.co/HO3vGvND9D pic.twitter.com/RcOpNo9MTM
Welcome High DPI usersLink to this section
Do you know that you can monitor your users with High DPI screens?
You can check it and make your app better for High DPI screens with the native media tags:
?#AngularTip for the day! You can make an #angular structural directive for adding content only if the user has High DPI display
— Roman Sedov (@marsibarsi) June 17, 2020
? It is convenient to use with video or picture sources pic.twitter.com/ejNeENGoyI
Do not forget about pipes!Link to this section
Pipes is a very powerful option to work in Angular way. It allows us to follow the declarative approach inside component templates. It saddens me that some Angular devs avoid creating their own pipes because you can make pipes almost for any case of data transformation.
And here is an example of a common pipe for many cases:
?#AngularTip for the day! Make a simple #angular mapper pipe that transforms value in a template with pure functions. Here is a well-typed sample by me
— Roman Sedov (@marsibarsi) June 10, 2020
⬆️This way Angular runs change detection only when mapper parameters have changed
Stackblitz demo:https://t.co/C5ddi9GQu4 pic.twitter.com/i7vhJnYtGz
Get your box with bananaLink to this section
It has so many names: banana in a box, two-way binding, input-output. And it is convenient to use it, for example, working with ngModel.
But how to implement it for your own component?
#AngularTip for the day! Do you know that you can make "Banana in a box" input-output in your Angular component?
— Roman Sedov (@marsibarsi) July 16, 2020
Just add "Change" postfix for the output name: pic.twitter.com/mxkrR455XT
RxJS is an unexplored worldLink to this section
I try to check all parameters and overloads of RxJS operators when I use them. The reason is: there are many hidden options knowing which you can write your streams faster and more powerful.
There are also many operators that are not very popular but can solve your particular problem in one line of code instead of a big stream.
One of such discoveries for me was the following one:
?#AngularTip for the day! #rxjs has many useful details, do not forget to check all parameters of rxjs operators.
— Roman Sedov (@marsibarsi) June 27, 2020
For example, did you know that you can use your own "compare" function into distinctUntilChanged operator? pic.twitter.com/5ldGEcHVrX
There is also a popular tweet about RxJS. It is a small trick to make your components a bit more scalable :)
?#AngularTip and #rxjsTip for the day! it is a good practice to make subjects private in public API and to provide an observable for class users.
— Roman Sedov (@marsibarsi) June 30, 2020
? You can use .pipe() instead of .asObservable(). The result is the same but .pipe() is more easy to expand in the future pic.twitter.com/LqFNf4gjjB
Or the other one: an operator that can help write tiny .pipe
with quite serious functionality.
?#AngularTip and #rxjsTip for the day! Do you know "defaultIfEmpty" operator? It emits default value if source completes without having emitted any value pic.twitter.com/17aezKM0wo
— Roman Sedov (@marsibarsi) June 29, 2020
You can even make your own ngFor alternativeLink to this section
And the last one: Angular has a special syntax for for … of …
like directives. This way you can create your own ngFor
for your case.
It can be, for example, a ngFor
for iterating a map. Or a simple for
for iterating from a number to a number:
?#AngularTip for the day! You can make an #angular directive similar to ngFor but using a number of repetitions rather than an array
— Roman Sedov (@marsibarsi) June 9, 2020
?It can save your time creating calendars, pagination or anything else you need to repeat a certain number of timeshttps://t.co/0qtKJUzGVM pic.twitter.com/GsJ6MDOiKY
ConclusionLink to this section
Angular is big and there is a lot more to tell. I have many ideas for new tips and tricks to share my best practices with the community. So, if you are interested in such new tweets, follow me on Twitter and have a nice day!
Comments (0)
Be the first to leave a comment
About the author

Frontend Developer at Tinkoff.ru

About the author

Frontend Developer at Tinkoff.ru