<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Juliano</title>
    <description>The latest articles on DEV Community by Juliano (@julianobrasil).</description>
    <link>https://dev.to/julianobrasil</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F263336%2F00ed35dc-64f5-4e3c-b7f9-b92306184494.png</url>
      <title>DEV Community: Juliano</title>
      <link>https://dev.to/julianobrasil</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/julianobrasil"/>
    <language>en</language>
    <item>
      <title>Building recursively nested @angular reactive forms: a clean approach</title>
      <dc:creator>Juliano</dc:creator>
      <pubDate>Sat, 13 Jun 2020 20:50:59 +0000</pubDate>
      <link>https://dev.to/julianobrasil/writing-nested-recursive-angular-reactive-forms-a-clean-approach-57fk</link>
      <guid>https://dev.to/julianobrasil/writing-nested-recursive-angular-reactive-forms-a-clean-approach-57fk</guid>
      <description>&lt;h1&gt;
  
  
  The use case
&lt;/h1&gt;

&lt;p&gt;&lt;small&gt;&lt;em&gt;Side Note: this post was inspired by this &lt;a href="https://stackoverflow.com/questions/62352286/angular-deeply-nested-reactive-form-cannot-find-control-with-path-on-nested-for/62355590?noredirect=1#comment110349898_62355590" rel="noopener noreferrer"&gt;StackOverflow question&lt;/a&gt;&lt;/em&gt;&lt;/small&gt;&lt;/p&gt;

&lt;p&gt;Let's talk about the below behavior:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Faig9g56z7oslaeablirz.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Faig9g56z7oslaeablirz.gif" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you look carefully at the above animated gif you'll notice 2 important things about the displayed form behavior:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;It's dynamically created&lt;/li&gt;
&lt;li&gt;It can be recursively nested indefinitely&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;A static image will help us to better understand the scenario:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fhohsn9c259uqsazqsgei.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fhohsn9c259uqsazqsgei.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I've highlighted some repeating parts on the form above. The red parts are very interesting, so take your time looking at them. Have you notice something with them? It's recursively nested. You can see that Group 2 has a Nested Group 1 inside it and both of them are structurally identical. In fact, Group 2 and the Nested Group 1 have the same elements: conditions and the same action buttons bar.&lt;/p&gt;

&lt;p&gt;How would we build up a dynamically, recursively nested form like the one shown in the image above? What if we want to create and fill-up the form based on some data you load from a server? Keep reading and we'll get there in a minute.&lt;/p&gt;

&lt;p&gt;The complete code discussed here is available at &lt;a href="https://stackblitz.com/edit/httpsdevtojulianobrasilwriting-nested-recursive-angular-reactiv?file=src%2Fapp%2Fgroup-control%2Fgroup-control.component.ts" rel="noopener noreferrer"&gt;Stackblitz.com&lt;/a&gt;:&lt;br&gt;
&lt;iframe src="https://stackblitz.com/edit/httpsstackoverflowcoma623555906433166?view=preview" width="100%" height="500"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h1&gt;
  
  
  The pseudo component tree
&lt;/h1&gt;

&lt;p&gt;We'll build the following structure (in this "pseudo component tree" each label started with &lt;code&gt;app&lt;/code&gt; represents an angular component):&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;

app-group-control
    |
   app-action-buttons-bar
    |
   conditions:
     app-condition-form
     app-condition-form
     app-condition-form
     ...
    |
   groups:
     app-group-control
     app-group-control
     app-group-control
     ...


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In the above pseudo component tree, &lt;code&gt;app-group-control&lt;/code&gt; is an angular reactive form that contains an action bar and 2 &lt;code&gt;FormArray&lt;/code&gt; controls: &lt;code&gt;conditions&lt;/code&gt; and &lt;code&gt;groups&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now I want you to be a curious developer and notice that &lt;code&gt;groups&lt;/code&gt; is a collection of &lt;code&gt;app-group-control&lt;/code&gt; components. Imagine this: you're gonna a build a component: &lt;code&gt;app-group-control&lt;/code&gt;. In its template, among other things, it has a collection of... itself. It's a &lt;em&gt;composite component&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Let's dive a little in each part of this component.&lt;/p&gt;

&lt;h2&gt;
  
  
  Action bar
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Flpiwnoygy6kvo3zbt76m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Flpiwnoygy6kvo3zbt76m.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Inside &lt;code&gt;app-group-control&lt;/code&gt; the first thing we'll find is this action bar (&lt;code&gt;ActionButtonsBarComponent&lt;/code&gt; in &lt;a href="https://stackblitz.com/edit/httpsstackoverflowcoma623555906433166?file=src%2Fapp%2Faction-buttons-bar%2Faction-buttons-bar.component.ts" rel="noopener noreferrer"&gt;Stackblitz demo&lt;/a&gt;) that allows us to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add a &lt;code&gt;app-group-control&lt;/code&gt; to &lt;code&gt;groups&lt;/code&gt; &lt;code&gt;FormArray&lt;/code&gt; (let's call it a "nested group")&lt;/li&gt;
&lt;li&gt;Add a condition to the &lt;code&gt;conditions&lt;/code&gt; &lt;code&gt;FormArray&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;When the user clicks on the delete group button, it will ask the component that hosts this instance of &lt;code&gt;app-group-control&lt;/code&gt; to be removed&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is a very simple component with 3 &lt;code&gt;@Ouput()&lt;/code&gt;-decorated properties:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Output&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="nx"&gt;remove&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;EventEmitter&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Output&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="nx"&gt;addGroup&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;EventEmitter&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Output&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="nx"&gt;addCondition&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;EventEmitter&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Condition
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Flacajenhv9784advcidj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Flacajenhv9784advcidj.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The condition component (&lt;code&gt;ConditionFormComponent&lt;/code&gt; in &lt;a href="https://stackblitz.com/edit/httpsdevtojulianobrasilwriting-nested-recursive-angular-reactiv?file=src%2Fapp%2Fcondition-form%2Fcondition-form.component.ts" rel="noopener noreferrer"&gt;Stackblitz demo&lt;/a&gt;) is also a very simple component. But, it has an important thing you should notice: it implements the &lt;code&gt;ControlValueAccessor&lt;/code&gt; interface.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;ControlValueAccessor&lt;/code&gt; interface is what makes it possible for you to turn your components into angular &lt;code&gt;FormControl&lt;/code&gt;s. And like any &lt;code&gt;FormControl&lt;/code&gt;, they can be part of forms (for example, they can receive the &lt;code&gt;formControlName&lt;/code&gt; directive and be controlled by the available &lt;code&gt;@angular/forms&lt;/code&gt; utilities and methods).&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;ControlValueAccessor&lt;/code&gt; is also, what make it possible for us to nest forms in the most possible simple way. I've seen a lot of people doing crazy things to nest forms, like passing the parent &lt;code&gt;FormGroup&lt;/code&gt; to a child component in order to reuse it in the child component's form. Maybe it's a matter of style, but I'd strongly recommend anyone to just turn that child component (that contains the nested form) into a &lt;code&gt;FormControl&lt;/code&gt;. By doing that you'll be flattening the form's structure. I'd suggest two Kara Erickson videos if you want to learn more cool stuff about angular forms. Finish reading this post and, after that, scroll your page back to check out these amazing videos.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/xYv9lsrV0s4"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/CD_t3m2WMM8"&gt;
&lt;/iframe&gt;
&lt;/p&gt;
&lt;h2&gt;
  
  
  The wrapper form component
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fkdr6yjflmgeat2f7n5h2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fkdr6yjflmgeat2f7n5h2.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Finally we have this wrapper component, that contains the form that wraps everything (&lt;code&gt;GroupControlComponent&lt;/code&gt; in &lt;a href="https://stackblitz.com/edit/httpsdevtojulianobrasilwriting-nested-recursive-angular-reactiv?file=src%2Fapp%2Fgroup-control%2Fgroup-control.component.ts" rel="noopener noreferrer"&gt;Stackblitz demo&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;Basically, its template is:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;

&lt;span class="nt"&gt;&amp;lt;form&lt;/span&gt; &lt;span class="na"&gt;[formGroup]=&lt;/span&gt;&lt;span class="s"&gt;"_form"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;app-action-buttons-bar&lt;/span&gt; &lt;span class="na"&gt;(remove)=&lt;/span&gt;&lt;span class="s"&gt;"remove.emit()"&lt;/span&gt;
                          &lt;span class="na"&gt;(addGroup)=&lt;/span&gt;&lt;span class="s"&gt;"_addGroup()"&lt;/span&gt;
                          &lt;span class="na"&gt;(addCondition)=&lt;/span&gt;&lt;span class="s"&gt;"_addCondition()"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/app-action-buttons-bar&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;ng-container&lt;/span&gt; &lt;span class="na"&gt;formArrayName=&lt;/span&gt;&lt;span class="s"&gt;"conditions"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;app-condition-form&lt;/span&gt; &lt;span class="na"&gt;*ngFor=&lt;/span&gt;&lt;span class="s"&gt;"let c of _conditionsFormArray?.controls; index as j"&lt;/span&gt; 
                        &lt;span class="na"&gt;(remove)=&lt;/span&gt;&lt;span class="s"&gt;"_deleteCondition(j)"&lt;/span&gt;
                        &lt;span class="na"&gt;[formControlName]=&lt;/span&gt;&lt;span class="s"&gt;"j"&lt;/span&gt; 
                        &lt;span class="na"&gt;[formLabel]=&lt;/span&gt;&lt;span class="s"&gt;"'Condition ' + (j+1)"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/app-condition-form&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/ng-container&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;ng-container&lt;/span&gt; &lt;span class="na"&gt;formArrayName=&lt;/span&gt;&lt;span class="s"&gt;"groups"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;app-group-control&lt;/span&gt; &lt;span class="na"&gt;*ngFor=&lt;/span&gt;&lt;span class="s"&gt;"let s of _groupsFormArray?.controls; index as i"&lt;/span&gt;
                        &lt;span class="na"&gt;(remove)=&lt;/span&gt;&lt;span class="s"&gt;"_deleteGroupFromArray(i)"&lt;/span&gt; 
                        &lt;span class="na"&gt;[formControlName]=&lt;/span&gt;&lt;span class="s"&gt;"i"&lt;/span&gt;
                        &lt;span class="na"&gt;[formLabel]=&lt;/span&gt;&lt;span class="s"&gt;"'Nested Group '+ (i + 1) + ':'"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/app-group-control&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/ng-container&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If you check the code in Stackblitz, you'll notice that &lt;code&gt;&amp;lt;app-condition-form&amp;gt;&lt;/code&gt; has its own form inside. &lt;code&gt;&amp;lt;app-group-control&amp;gt;&lt;/code&gt; also has its own form (we're actually looking at it in the code above). Thanks to &lt;code&gt;ControlValueAccessor&lt;/code&gt;, nesting these two forms inside a parent form was a piece of cake. Take a look again... we're not only nesting two forms in a completely transparent way, which would be a huge achievement per se. Considering that &lt;code&gt;conditions&lt;/code&gt; and &lt;code&gt;groups&lt;/code&gt; are &lt;code&gt;FormArrays&lt;/code&gt;, we're, in fact, nesting an indefinite number of forms inside the parent form, without the need of any hack, using a simple, clean and robust approach. Pretty cool, don't you think?&lt;/p&gt;

&lt;h1&gt;
  
  
  The recursion
&lt;/h1&gt;

&lt;p&gt;Like if it wasn't awesome enough to nest an indefinite number of forms in the first nested level, we're &lt;strong&gt;nesting host component instances inside itself... for an indefinite number of nesting levels down&lt;/strong&gt;. It deserves a huge OMG!!! I hope this can give you a measure of the power of angular component-based structure.&lt;/p&gt;

&lt;h1&gt;
  
  
  Create the form based on incoming data
&lt;/h1&gt;

&lt;p&gt;As a part of the implementation of the &lt;code&gt;ControlValueAccessor&lt;/code&gt; interface, we implemented the &lt;code&gt;writeValue(...)&lt;/code&gt;. It is called by angular whenever the &lt;code&gt;FormControl.setValue()&lt;/code&gt; or &lt;code&gt;FormControl.patchValue()&lt;/code&gt; is executed by the host form. And this is a perfect place to analyze the incoming data and create/remove/fill-up controls of our form according to the incoming data.&lt;/p&gt;

&lt;p&gt;If you take a look at this &lt;a href="https://stackblitz.com/edit/httpsdevtojulianobrasilwriting-nested-recursive-angular-reacti2?file=src/app/group-control/group-control.component.ts" rel="noopener noreferrer"&gt;modified stackblitz demo&lt;/a&gt;, you'll see how we can achieve this with two tiny snippets (both of them in the &lt;code&gt;writeValue(...)&lt;/code&gt; functions of the &lt;code&gt;GroupControlComponent&lt;/code&gt;):&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="nf"&gt;writeValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Record&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;any&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;...&lt;/span&gt;
  &lt;span class="c1"&gt;// Here we create the `conditions` controls based on &lt;/span&gt;
  &lt;span class="c1"&gt;//   existent `conditions` attribute in the value&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;conditions&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;conditions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_conditionsFormArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;clear&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;conditions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;_addCondition&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;...&lt;/span&gt;
  &lt;span class="c1"&gt;// Here we create the `groups` controls based on &lt;/span&gt;
  &lt;span class="c1"&gt;//   existent `groups` attribute in the value&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;groups&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;groups&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_groupsFormArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;clear&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;groups&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;g&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;_addGroup&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;So, in the &lt;code&gt;AppComponent&lt;/code&gt; (in the demo), when we call &lt;code&gt;this._form.patchValue(data);&lt;/code&gt; in the &lt;code&gt;buildFormFromData()&lt;/code&gt; method, our form is built automatically to accommodate &lt;code&gt;data&lt;/code&gt; in the right fields. With &lt;code&gt;ControlValueAccessor&lt;/code&gt; in place, this was pretty easy, wasn't it?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F946dpgfdhuswwpi2si11.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F946dpgfdhuswwpi2si11.gif" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Final Considerations
&lt;/h1&gt;

&lt;p&gt;If you try to build this same form without angular &lt;code&gt;ControlValueAccessor&lt;/code&gt;, I can guarantee you painful headaches during all the process. For some reason, the &lt;code&gt;ControlValueAcessor&lt;/code&gt; is one of the most underestimated angular features among developers. Not using it makes the overall process of building complex forms an overwhelming task.&lt;/p&gt;

&lt;h2&gt;
  
  
  Give my schematics library a try
&lt;/h2&gt;

&lt;p&gt;BTW, I've built an schematics libray that generates the skeleton of a component with the &lt;code&gt;ControlValueAccessor&lt;/code&gt; implementation for you. If you want to, fell free to try it in your projects (it's a &lt;code&gt;devDependency&lt;/code&gt;, so it won't add any nano dependency to your project): &lt;a href="https://www.npmjs.com/package/@julianobrasil/schematics-components" rel="noopener noreferrer"&gt;https://www.npmjs.com/package/@julianobrasil/schematics-components&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Credits: Cover image from &lt;a href="https://undraw.co" rel="noopener noreferrer"&gt;https://undraw.co&lt;/a&gt;&lt;/p&gt;

</description>
      <category>angular</category>
      <category>reactiveforms</category>
      <category>nestedforms</category>
      <category>controlvalueaccessor</category>
    </item>
    <item>
      <title>It's something @angular injectors can solve</title>
      <dc:creator>Juliano</dc:creator>
      <pubDate>Wed, 01 Apr 2020 14:05:49 +0000</pubDate>
      <link>https://dev.to/julianobrasil/it-s-something-angular-injector-can-solve-243k</link>
      <guid>https://dev.to/julianobrasil/it-s-something-angular-injector-can-solve-243k</guid>
      <description>&lt;h1&gt;
  
  
  The use case
&lt;/h1&gt;

&lt;p&gt;Today I answered &lt;a href="https://stackoverflow.com/q/60926745/6433166"&gt;a question at StackOverflow&lt;/a&gt; and I'll describe it here just because the author is free to remove it from there breaking that link. The question was: &lt;strong&gt;&lt;em&gt;How to Access Parent Component from Child Component in Angular 9&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The first point is that component interaction is very well &lt;a href="https://angular.io/guide/component-interaction"&gt;documented on @angular official site&lt;/a&gt;. I'm not the hugest fan of what the author of the question wanted to do. I'm not gonna judge his reasons too because I really don't know his use case thoroughly though. And, most of all, angular provides the tools to do that, so why not learning how to use them? Someday they can be useful. Let's just, for a moment, forget that there are other ways of solving the problem we're taking as an excuse for our sleuthing and focus on this topic: how to use @angular injector &lt;em&gt;directly&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;So, in the description of the question, the author described this scenario:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;template&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;&amp;lt;child&amp;gt;&amp;lt;/child&amp;gt;&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Parent&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;pp&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;selector&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;child&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;template&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;&amp;lt;grand-child&amp;gt;&amp;lt;/grand-child&amp;gt;&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Child&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="na"&gt;selector&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;grand-child&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...})&lt;/span&gt; &lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;GrandChild&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What he wanted to do was accessing, from the &lt;code&gt;GrandChild&lt;/code&gt;, the &lt;code&gt;Parent&lt;/code&gt; to change &lt;code&gt;pp&lt;/code&gt; property. And he was asking why he wasn't able to do, in angular 9, what he was used to doing in angular 8. In his description, he describes what he was trying to do and I'll post it here as it was in the question text:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;viewContainerRef&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;_data&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;componentView&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;component&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;viewContainerRef&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;_view&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;component&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Don't mess with the library private properties
&lt;/h2&gt;

&lt;p&gt;If you don't wanna be lectured because of one seriously bad coding practice, just jump to the next section&lt;/p&gt;

&lt;p&gt;Don't be distracted by following the logic in the last snippet: it's not important if it works or not. The point is that in a serious typescript project like angular, you should believe when the developer uses the private modifier. What he is trying to say is: this property is part of the internal implementation of this class and I can change it without any warning.&lt;/p&gt;

&lt;p&gt;Keep this abbreviation in mind: API. The API (&lt;em&gt;Application Program Interface&lt;/em&gt;) is the thing you should rely on. As its name said, it is supposed to be the way of an application to use the features of a class/library. Changes in the API are usually avoided by the libraries' authors. And when they are inevitable, they first try to make it compatible with previous versions, and if not possible, publish a document listing that change as a &lt;em&gt;breaking change&lt;/em&gt; (because some of the users of that library will have to change their codes in order to use that version).&lt;/p&gt;

&lt;p&gt;The API is so important that some testers say that they are the only part of a library that &lt;em&gt;must&lt;/em&gt; be tested. It cannot break.&lt;/p&gt;

&lt;p&gt;So, if you find yourself in a situation where you need something that is not on the API, the best you can do is asking the authors to expose it. If the authors agree with that, let's consider as acceptable to temporarily use hacks while you wait for that implementation to be rolled out with the new API adding. If the authors aren't gonna change the API, find another way to do what you want. Keeping code that accesses private properties of 3rd party libraries, is having a bomb with a random timer in your project: sooner or later, without any warning, it'll give you headaches.&lt;/p&gt;

&lt;p&gt;If my reasons didn't convince you to not use those private properties, do yourself a favor: backup the piece of code you're using it with good tests (and comments). Don't let your users find out for you that a part of your app isn't working anymore.&lt;a id="angular-dependency-injection"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  @angular Dependency Injection
&lt;/h2&gt;

&lt;p&gt;There are great articles out there (and the &lt;a href="https://angular.io/guide/dependency-injection"&gt;official docs&lt;/a&gt;) diving into how @angular's dependency injection (DI) system works and describing how it uses a probabilistic search technique (Bloom Filters) to discover the injectable stuff in the angular component tree, like &lt;a href="https://medium.com/angular-in-depth/angular-di-getting-to-know-the-ivy-nodeinjector-33b815642a8e"&gt;this one&lt;/a&gt;. It's not part of the scope of this article to go through all that information again.&lt;/p&gt;

&lt;p&gt;You only have to know that the DI has multiple &lt;em&gt;injectors&lt;/em&gt;, which are methods, associated with components, directives, pipes, and modules, that are responsible for look for objects based on tokens throughout the DI system. For example, if you ask a specific injector for a component and it cannot find it, it asks its parent injector for that component and so on. The injectors are distributed in a hierarchical tree.&lt;/p&gt;

&lt;h1&gt;
  
  
  How can the angular DI solve that stackoverflow problem
&lt;/h1&gt;

&lt;h2&gt;
  
  
  The most simple way
&lt;/h2&gt;

&lt;p&gt;Angular allows for any parent component to be directly injected in any of its children. It's simple and effective. End of discussion.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;template&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;&amp;lt;child&amp;gt;&amp;lt;/child&amp;gt;&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Parent&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;pp&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;selector&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;child&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;template&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;&amp;lt;grand-child&amp;gt;&amp;lt;/grand-child&amp;gt;&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Child&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="nx"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Parent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="na"&gt;selector&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;grand-child&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...})&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;GrandChild&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kr"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;_parent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Child&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_parent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;some value&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Using ViewContainerRef service
&lt;/h2&gt;

&lt;p&gt;All angular component can be used as a reference in the dom to create other components dynamically. The &lt;code&gt;ViewContainerRef&lt;/code&gt; is a service associated a component that has methods to do that creation taking that component as a reference in the DOM (even though it may appear that these dynamic components are created inside the component that owns the &lt;code&gt;ViewContainerRef&lt;/code&gt;, it is, in fact, created as a sibling - see &lt;a href="https://netbasal.com/angular-2-understanding-viewcontainerref-acc183f3b682"&gt;this article&lt;/a&gt; for more info).&lt;/p&gt;

&lt;p&gt;What we are really interested in here is the fact that the &lt;code&gt;ViewConainerRef&lt;/code&gt; service has a public method to get the parent's injector of the component it is associated with. _And it can also be injected in the component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;template&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;&amp;lt;child&amp;gt;&amp;lt;/child&amp;gt;&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Parent&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;pp&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;selector&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;child&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;template&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;&amp;lt;grand-child&amp;gt;&amp;lt;/grand-child&amp;gt;&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Child&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="na"&gt;selector&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;grand-child&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...})&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;GrandChild&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kr"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;_viewContainerRef&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ViewContainerRef&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;childInjector&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_viewContainerRef&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parentInjector&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Parent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;childInjector&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Parent&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Parent&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;some value&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that we don't need any property on &lt;code&gt;Child&lt;/code&gt; component to get to the &lt;code&gt;Parent&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using Injector service
&lt;/h2&gt;

&lt;p&gt;If you look carefully at the previous technique, you may connect some dots and think to your self that if there's a tree of injectors and if one injector doesn't know how to resolve a reference token, it asks for its parent injector... than why just ask the component's injector instead of asking the parent injector for that info? And it's a completely plausible question. Of course, you can do so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;template&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;&amp;lt;child&amp;gt;&amp;lt;/child&amp;gt;&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Parent&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;pp&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;selector&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;child&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;template&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;&amp;lt;grand-child&amp;gt;&amp;lt;/grand-child&amp;gt;&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Child&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="na"&gt;selector&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;grand-child&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...})&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;GrandChild&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kr"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;_injector&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Injector&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Parent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_injector&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Parent&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Parent&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;some value&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that, once more, we don't need any property on &lt;code&gt;Child&lt;/code&gt; component to get to the &lt;code&gt;Parent&lt;/code&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Final Considerations
&lt;/h1&gt;

&lt;p&gt;In the above cases, you can get a reference for the components, because they are hierarchically distributed. It wouldn't work, for example, if &lt;code&gt;A&lt;/code&gt; and &lt;code&gt;B&lt;/code&gt; components were siblings (you would not be able to inject a reference to &lt;code&gt;A&lt;/code&gt; using &lt;code&gt;A&lt;/code&gt;'s injector =&amp;gt; the search algorithm look for a token up in a tree from a given start point, it won't look for a token going up in the tree and, then, going down from upper nodes). You couldn't, also, inject &lt;code&gt;B&lt;/code&gt; in &lt;code&gt;A&lt;/code&gt; if &lt;code&gt;B&lt;/code&gt; was a child of &lt;code&gt;A&lt;/code&gt; (the injectors asks information up in the hierarchy, not down in it).&lt;/p&gt;

&lt;p&gt;Knowing that you have an injector at your service sometimes solves some problems in a fast way (maybe not the best way, but in a fast way). Maybe you'll never use it, but it's there for you. &lt;/p&gt;

</description>
      <category>angular</category>
      <category>dependencyinjection</category>
      <category>componentcommunication</category>
    </item>
    <item>
      <title>Prepare your @angular web app to grow: keep it performant and consistent from the beginning</title>
      <dc:creator>Juliano</dc:creator>
      <pubDate>Wed, 25 Dec 2019 22:01:35 +0000</pubDate>
      <link>https://dev.to/julianobrasil/prepare-your-angular-web-app-to-grow-keep-it-performant-and-consistent-from-the-beginning-50kp</link>
      <guid>https://dev.to/julianobrasil/prepare-your-angular-web-app-to-grow-keep-it-performant-and-consistent-from-the-beginning-50kp</guid>
      <description>&lt;h1&gt;
  
  
  Summarizing...
&lt;/h1&gt;

&lt;p&gt;If you're using angular, I assume that you like its CLI opinionated project structure. It's well designed to give you a good start point for a web application. At the same time, it's flexible enough to give you the freedom to do all sorts of crazy assemblies. If you, at the moment, don't care about how you're doing things just because you feel comfortable in a supposedly safe directory structure built by Angular CLI, chances are that, in the long term, you'll end up with an unmaintainable slow web application haunting your life.&lt;/p&gt;

&lt;h2&gt;
  
  
  What could go wrong?
&lt;/h2&gt;

&lt;p&gt;It's not uncommon to underestimate the potential of a web app during the time it's being built. Once it is released and people start using it, features requests are almost unavoidable. Have you missed something during your app specs? Even though a poor analysis is usually the reason for an incomplete final product, this is not always the root cause of this kind of situation. The list of what could possibly take your original app to this undesirable stage is larger than just one single poor-analysis factor. Maybe the application was formerly designed to quickly fulfill a single tiny specific demand in the company where you work. Or maybe the client changed his/her mind after seeing the released product; or maybe it was just a pet project that started showing some real potential. It doesn't matter right now what is pushing you to change your original application. In this post, let's focus on the product developing practices, instead of concentrating on the reasons that led it to its current (messy?) state. Let's talk about how you can build or refactor your app, even if it is a small one today, to make it less painful to expand it in the future. We'll be focusing on an @angular web app. We'll see a few good practices that, if adopted from the beginning, can keep your application architecture healthy. Despite the fact that they are kind of overwhelming in small apps (IMO), they can be your life-guard in the future.&lt;/p&gt;

&lt;h3&gt;
  
  
  What we'll be talking about
&lt;/h3&gt;

&lt;p&gt;There are some actions you can take to keep an @angular app healthy as time goes by. This article does not intend to be an exhaustive list of good practices, but a shortlist of those that I think are the most important of them. We'll be talking about &lt;em&gt;lazy loading&lt;/em&gt;, &lt;em&gt;state management&lt;/em&gt;, &lt;em&gt;change detection&lt;/em&gt;, &lt;em&gt;components architecture&lt;/em&gt; and &lt;em&gt;schematics&lt;/em&gt;. And the kind of situations a wrong decision involving these topics can bring to the final product. I'll try to reference relevant articles and pages about each topic as we go through each one of them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lazy Loading Modules
&lt;/h2&gt;

&lt;p&gt;This is a UX tool available for your app. As your app grows, you can just split it up in several modules.&lt;/p&gt;

&lt;p&gt;Any component with some complexity above the minimum should be declared in its own module. I'm calling "complex" any component that you have to spend 200+ lines (not taking comment lines into account) of code to make it works. Maybe a form with only two fields doesn't need to reside in its own module, but a popup list with a searching feature certainly does. By declaring them in their own modules, you can import more selectively what you take into your app. Of course, there's no need for lazy loading complex components. The real advantage of lazy loading is on feature modules.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The problem:&lt;/strong&gt; if you don't lazy load your feature modules (virtually all of them), you are forcing all of your users to download all the files of your application even if they won't ever use most of the features being downloaded to their browsers. We're talking about the application first rendering delay, that time spent from the moment the user sends a request to the server to the time he/she sees something in the browser. I'm not talking just about that splash screen, but about what comes &lt;em&gt;after&lt;/em&gt; it. Instead of forcing the users to stare at a splash screen like forever waiting for the first useful module to be loaded, you want your app to be ready to use ASAP.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The solution:&lt;/strong&gt; Letting the user initially download just what he needs at first is a bandwidth saver. This can be a very sensible aspect on devices with a poor internet connection. Maybe you want to initially send just that login-page component and nothing more. As soon as the user can see that component you can setup @angular to automatically download (in the background) the next modules that he/she will probably want to see after logging in. Of course, you can opt to wait for the user to finish the login process before start downloading anything else. Or you can take the lazy loading to another level by analyzing the user's average behavior using some statistical tool to try to predict what parts of the app your users will probably want to see after any visited page, by using Minko's &lt;a href="https://guess-js.github.io/docs/angular"&gt;Gess.js&lt;/a&gt; like he describes in &lt;a href="https://web.dev/route-preloading-in-angular/"&gt;this great article&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Caveats:&lt;/strong&gt; We are talking exclusively about UX, not about security. Developers tend to believe they are increasing security by using lazy loading and angular routes guards. The ability to load a module just after you're sure the logged user has the right permissions to do it doesn't mean you're protecting anything. You shouldn't rely on the front-end features at all to secure your app. The back-end is the right place to take your security actions because it's much harder for an attacker to have access to it in order to mess up with your system. On the other hand, the front-end code is fully available to be explored. One more persistent user with some javascript knowledge will find out how to reveal hidden components or use your REST API from Postman or any equivalent tool to try to gain access to your database and other protected resources.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Recommended Reading:&lt;/strong&gt; You can learn a lot about lazy loading from the links below.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://angular.io/guide/lazy-loading-ngmodules"&gt;Setting up lazy load modules&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://angular.io/guide/router#milestone-6-asynchronous-routing"&gt;Asynchronous routing&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://web.dev/route-level-code-splitting-in-angular/"&gt;Route-level code splitting in Angular&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://juristr.com/blog/2019/04/state-lazy-loading-components-angular/"&gt;Yes, you can load individual components lazily too&lt;/a&gt; &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Get used to loading all of your feature modules lazily. By doing that it'll be easier to control what your users need to download when they use your web app.&lt;/p&gt;

&lt;h2&gt;
  
  
  State Management
&lt;/h2&gt;

&lt;p&gt;This is a consistency tool in some situations and a UX one in others. As your app grows, you'll need to show the same information in (potentially) various formats to the user. For example, you can let the user enter a new address in a profile form and want that address to be instantly available on a list shown in an area on the page just reserved to show existing addresses or to be available in a selection list immediately&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The problem:&lt;/strong&gt; if you have the same information spread all over the place in your app, sooner or later you'll forget to update it somewhere and you'll be face-to-face with a problem similar to the &lt;a href="https://blog.pusher.com/the-what-and-why-of-redux/"&gt;wrong-messages-count&lt;/a&gt; one that led facebook to adopt redux.  You want the information presented to your user in various forms to be consistent. This is one problem. The other situation is about UX. Suppose you have this really long form. Suppose a user is in the middle of the task of filling up that form. Suppose that, for any reason, the user needs to navigate away from the form page (not just hiding it, but destroying it). Finally, suppose the user navigates back to the form: would it be a good UX if he/she find out that the already fulfilled information was all cleared up and it's necessary to start filling up the form all over again?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The solution:&lt;/strong&gt; have a state object to store all your app components' currently used values. One single object containing all the information, without duplicates. If you google it you'll find out that this single object is called "single source of truth" and it's basically an object that can be globally accessed by any component. It's also important to notice that it's easier to add developers to a project if you follow (well-established) patterns. Of course, you could build this global state object by yourself, using reactive functions from the &lt;code&gt;rx.js&lt;/code&gt; library, like BehaviorSubject's, ReplaySubject's, etc. Then you'd have to write functions to isolate the pieces of the state object you wanted in each part of your app. Soon you'd need functions specialized in updating the state object. And you'd have to decide whether mutate it was a better idea than just create a new object. Next, for the sake of organization, you'd decide to have functions to be run as a consequence of the state object updates. Then you'd finally realize you'd be reinventing the wheel and that you should have adopted, from the beginning, NGRX, NGXS, Akita or any other stable library that do all of what you'd have been coding for months just out-of-the-box.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Caveats:&lt;/strong&gt; the amount of code you have to write to get a state object up and running can be intimidating. It's not avoidable and let's face it: it's really a lot of work to do in terms of coding, apparently repeated (although it's not really duplicated code despite the fact it &lt;em&gt;appears&lt;/em&gt; to be so). But it has been a while since coding a lot became part of a developer's job. I have a clear impression that in the past IDEs did a lot of the front-end job. Today it seems that the heavy lifting has been transferred to the programmer, so... what are we complaining about? It's a lot of code that will save us from a lot of known issues that can be irrelevant and easy to deal with while the app is small, but will certainly bring up terrible headaches when it becomes a large web app.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Recommended Reading:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://ultimatecourses.com/blog/redux-typescript-store"&gt;Understand Redux by building your own Store&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://ultimatecourses.com/blog/ngrx-store-understanding-state-selectors"&gt;NGRX Store: Understanding State Selectors&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/playlist?list=PLW2eQOsUPlWJRfWGOi9gZdc3rE4Fke0Wv"&gt;Video series about NGRX&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://ngrx.io/"&gt;NGRX&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.ngxs.io/"&gt;NGXS&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/datorama/akita"&gt;Akita&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  OnPush Change Detection Strategy
&lt;/h2&gt;

&lt;p&gt;One of the reasons &lt;code&gt;@angular&lt;/code&gt; has been originally designed to be a completely different project from &lt;code&gt;angular.js&lt;/code&gt;, is that the latter's two-way data flow used frequently by the change detector. In &lt;code&gt;@angular&lt;/code&gt;, you're not forced to use two-way data binding, so an update in a child doesn't necessarily cause an update on the data in a parent component (you can use &lt;code&gt;@Input()&lt;/code&gt; and &lt;code&gt;@Output()&lt;/code&gt; decorators instead of "banana-in-a-box" notation to exchange data). But the two-way data binding alone is not a big problem. Think of a page full of components, having a large table and lots of animating things triggered by user interactions. Now imagine &lt;code&gt;@angular&lt;/code&gt; having to figure out which parts of that page (comprised of lots of components) must be updated so the view can reflect correctly the underlying data. You, as the developer, know exactly what should be refreshed on the page whenever the underlying data changes. @angular's change detector does it's best to repeat the same task as you'd do if you could take care of the change detection process.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The problem:&lt;/strong&gt; In the process of figuring out which parts of the view should be updated, @angular updates almost everything related to data in almost every situation. In heavy pages, this can turn the UX into a hell. The page can potentially take seconds to update every part and, in the process, disallow any interaction of the user with any part of it. Of course, you don't want to take care of selecting which components should be updated in that same heavy page, because it surely has many components being presented. But certainly, you'd wish to control this updating process at a higher level, telling @angular &lt;em&gt;which groups of components&lt;/em&gt; (in contrast to "which components") and &lt;em&gt;when&lt;/em&gt; they should be updated to correctly reflect the underlying data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The solution:&lt;/strong&gt; to have more control over &lt;em&gt;when&lt;/em&gt; the change detection should affect your component, you can tell angular to automatically runs the change detector in some special cases (&lt;code&gt;@Input()&lt;/code&gt; change events, async pipes being updated and DOM originated events). This can be activated by setting the &lt;code&gt;@Component&lt;/code&gt;'s attribute &lt;code&gt;changeDetection&lt;/code&gt; to &lt;code&gt;ChangeDetectionStrategy.OnPush&lt;/code&gt;. It will probably have no noticeable performance improvement in simple components, but I assure you it's a lifesaver when you're dealing with complex components comprised of several subcomponents. This can be the difference between a good UX and a completely unusable page of your app. If you take a look at well-designed components libraries like &lt;a href="https://github.com/angular/components"&gt;Google's &lt;code&gt;@angular/components&lt;/code&gt;&lt;/a&gt; you'll notice that &lt;em&gt;all&lt;/em&gt; of the components have their change detection strategy set to &lt;code&gt;OnPush&lt;/code&gt;. It's an important clue of what you should be doing. All of your components should follow the same strategy (IMO). Most of the times this strategy will be enough for any component. In some specific situations not covered by &lt;code&gt;OnPush&lt;/code&gt; strategy, you can inject a reference to the change detector (&lt;code&gt;ChangeDetectorRef&lt;/code&gt;) and express your will to force a component to take part in the next turn of change detection by calling its &lt;code&gt;markForCheck&lt;/code&gt; method inside this component. To group your components... well, it's more simple than you imagine: you can build a component to group them, because, in @angular, everything is a component.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Caveats:&lt;/strong&gt; most of the time, there aren't any points you should be aware of when using &lt;code&gt;OnPush&lt;/code&gt; (just be careful to not invalidate the OnPush strategy by too many manual calls to the change detector). I'll update this article if I remember something.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Recommended Reading:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://angular.io/api/core/ChangeDetectorRef"&gt;Angular API&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/mokkapps/the-last-guide-for-angular-change-detection-you-ll-ever-need-3pe4"&gt;The Last Guide For Angular Change Detection You'll Ever Need&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Components Architecture (Container-Presentational)
&lt;/h2&gt;

&lt;p&gt;Right now you have two important pieces of the architecture I'm suggesting: state management and &lt;code&gt;OnPush&lt;/code&gt; change detection strategy. You'll want to use them at their full potential and there's one thing you can do to assure it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The problem:&lt;/strong&gt; at this point, if you follow carefully the preceding sections, you're probably mentally set up your projects to use the above techniques. It is possible you'll not take full advantage of them and maybe the OnPush change detection strategy brings you some headaches in some situations, forcing you to manually call the change detector two many times. In addition to that, you have a component hard to test because of the reactive nature of the injected services that will make your project work.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The solution:&lt;/strong&gt; there's a recommended way of organizing your project components in order to take maximum advantage of the configuration I've written bout until now. It's called &lt;em&gt;Container-Presentational&lt;/em&gt;. It's important to say that this isn't general advice for every framework. We're speaking strictly of &lt;code&gt;@angular&lt;/code&gt; here, in the specific situation where the components are set to use the &lt;code&gt;OnPush&lt;/code&gt; strategy. Long story short, this is about grouping most of your components into two categories:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Presentational&lt;/strong&gt;: the components in this category, as a rule, are not allowed to have any features but those related to rendering the information on the screen. The presentational components shouldn't have any injected services or access any rest API. All of the data the component needs in order to work correctly should be passed in by &lt;code&gt;@Input()&lt;/code&gt; properties (which, in turn, activates the change detector, even in OnPush strategy, automatically). Similarly, all the data and requests the component needs to externalize should be done using &lt;code&gt;@Output()&lt;/code&gt; properties.  You are allowed to inject a component service, containing methods strictly related to the rendering process or the logic of showing/hiding the component's parts. Sometimes you'll find authors using the term &lt;em&gt;dumb components&lt;/em&gt; to describe &lt;em&gt;presentational components&lt;/em&gt; (the &lt;em&gt;smart components&lt;/em&gt; would be the other category, described below).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Container&lt;/strong&gt;: the components in this category, are responsible to grab the data from external sources (REST APIs/Global State Objects) and to pass the information down to presentational components. Here you are allowed to inject as many services you think are necessary, including the &lt;em&gt;State Management&lt;/em&gt; service. They act as the "API" components for your project. If your project is split into several libraries, the Container components would be used directly on your feature modules, and they would wrap your presentational components, passing data down to them in order to make your project work.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Caveats:&lt;/strong&gt; sometimes it's not so obvious if a component should be container or presentational. The most obvious situation is the dialogs. On some occasions, they can be considered presentational and, in others, container components. There's not any strong rule stating what they should act like. In fact, once you're using this container-presentational organization, nobody would be arrested for deciding to inject the store service in a formerly presentational component. There are legitimate use cases where it's much more simple to inject the store in a presentational component than pass in 15 input data, which would increase the complexity of the component API. So, don't worry if you exceptionally have to inject some services in your presentational components.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Recommended Reading&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://blog.angularindepth.com/container-components-with-angular-11e4200f8df"&gt;Container Components with Angular&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.angular-university.io/angular-2-smart-components-vs-presentation-components-whats-the-difference-when-to-use-each-and-why/"&gt;Angular Architecture - Smart Components vs Presentational Components&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.angular-university.io/angular-component-design-how-to-avoid-custom-event-bubbling-and-extraneous-properties-in-the-local-component-tree/"&gt;Angular Architecture - Container vs Presentational Components Common Design Pitfalls&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/@dan_abramov/smart-and-dumb-components-7ca2f9a7c7d0"&gt;Presentational and Container Components - React Article&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;@angular/cli&lt;/code&gt; schematics: thinking of DX
&lt;/h2&gt;

&lt;p&gt;To take to your project all the things we've been talking about, you'll surely add a lot of code. If your project is already on production or in an advanced stage of development, we're also talking about refactoring (even more work to do). In any situation, when building a new project or refactoring an existing one, every developer should always think of how to reduce the amount of code needed to make things work.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The problem:&lt;/strong&gt; adding a robust and flexible architecture to any software project usually takes many lines of code plus the creation of some files. We're talking about interfaces, abstract classes, services, DI, tests, etc. To keep your code healthy and consistent, you will add things to your code that once in a while will seem overwhelming ("OMG, this is just a hello-world component! Why do I have to create so many files/interfaces/services/modules?"). Believe-me: you should be faithful to the chosen design patterns even in these apparently overwhelming scenarios. At the same time, you should find a way to not kill the developers' experience and productivity and let them do what they are meant to do: coding. Most of the work should be around thinking of the business logic instead of writing code because there's a specific design pattern to be followed. Yes, we're talking about how to avoid the inevitable &lt;em&gt;boilerplate&lt;/em&gt;. The design pattern should get into the project in the most possible transparent way.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The solution:&lt;/strong&gt; there are some actions you could take to reduce the amount of boilerplate. Use good code editors configured correctly using the same setup shared among the team, for example. But I'd like to talk here about a powerful tool that comes with &lt;code&gt;@angular/cli&lt;/code&gt;: schematics. &lt;code&gt;Schematics&lt;/code&gt; is a template-based tool that comes with an API, that allows you to manipulate text files. Using schematics you are able to take one or more template files and generate custom commands that take those template files and generate other files based on them, replacing specific parts of their contents by whatever you want. &lt;code&gt;Schematics&lt;/code&gt; turn the boring task of creating complex directory structures in a very quick and productive task. And the best part is that building your custom &lt;code&gt;schematics&lt;/code&gt; is not that hard, even if you have never done anything like that before. &lt;code&gt;@angular/cli&lt;/code&gt; itself uses &lt;code&gt;schematics&lt;/code&gt; to generate components, modules, pipes, etc. What Google did here is to turn all the heavy work that &lt;code&gt;@angular/cli&lt;/code&gt; did internally into a powerful API. How about creating a template to, in 2 or 3 seconds, generate a feature module, with a component inside, ready to be lazy-loaded, with all the routing already configured to use &lt;code&gt;@angular/material&lt;/code&gt; imported as a separated module and a component service injected on your component? Try to build it and you'll see that to an experient developer who's also a fast typer, knowing absolutely what to do, it will take like 5 to 10 minutes. To a beginner developer, it would easily take 40 to 60 minutes. Building some custom schematics to help your day-by-day coding can be a good idea.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Caveats:&lt;/strong&gt; be careful to not catch your self reinventing the wheel. The schematics won't ever be a peer dependency on your project (they're always a dev-dependency). So do not be afraid of &lt;code&gt;npm install --save-dev&lt;/code&gt; 3rd parties schematics libraries. If their projects become abandoned, you can stop using them whenever you want without refactoring any code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Recommended Reading:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://angular.io/guide/schematics-for-libraries"&gt;Schematics for Libraries&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/angular/the-7-pro-tips-to-get-productive-with-angular-cli-schematics-157m"&gt;The 7 Pro Tips To Get Productive With Angular CLI &amp;amp; Schematics&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/thisdotmedia/schematics-building-blocks-2mg3"&gt;Schematics: Building blocks&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=X06tuCohJPQ"&gt;Video - 2019: A Schematic Odyssey | Kevin Schuchard &amp;amp; Brian Love&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=y6uSJ4a5sMA"&gt;Global Thermonuclear Templates - Mike Brocchi&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;You should get to know all the pros and cons of the approaches you choose for your components. You'll find out that simple strategies, like the ones shown here, can help to keep your application healthy during its active life span.&lt;/p&gt;

</description>
      <category>angular</category>
      <category>performance</category>
      <category>statemanagement</category>
      <category>goodpractices</category>
    </item>
    <item>
      <title>Accessing angular components methods from within Cypress</title>
      <dc:creator>Juliano</dc:creator>
      <pubDate>Mon, 23 Dec 2019 18:13:46 +0000</pubDate>
      <link>https://dev.to/julianobrasil/accessing-angular-components-methods-from-within-cypress-4imf</link>
      <guid>https://dev.to/julianobrasil/accessing-angular-components-methods-from-within-cypress-4imf</guid>
      <description>&lt;h2&gt;
  
  
  Some Initial thoughts
&lt;/h2&gt;

&lt;p&gt;One important (let's avoid the word "essential" here) element to keep a long-living application maintainable is the presence of tests. It's a common-sense that they are a must-have piece of any healthy codebase and come in some flavors: &lt;strong&gt;Unit&lt;/strong&gt;, &lt;strong&gt;Integration&lt;/strong&gt;, and &lt;strong&gt;Functional&lt;/strong&gt; (end-to-end or e2e) tests. You can read more about each one on &lt;a href="https://medium.com/welldone-software/an-overview-of-javascript-testing-in-2019-264e19514d0a"&gt;this great article&lt;/a&gt;. There are two kinds of elements to be tested: ui and non-ui components (in the former case, you can perform visual and/or non-visual tests). The adoption of one or more types of tests and the corresponding tools that will make it possible to run them automatically depends on the project and its natural constraints (team expertise, team size, deadlines, adopted technologies, presence or absence of CI/CD, etc). &lt;/p&gt;

&lt;p&gt;In JavaScript world, like in other languages, we have different tools for each type of test (unit, integration, e2e).&lt;/p&gt;

&lt;h2&gt;
  
  
  A few words about unit tests
&lt;/h2&gt;

&lt;p&gt;As of the writing of this post, we typically use Jest/Karma to perform &lt;strong&gt;unit testing&lt;/strong&gt; (static, isolated, and integration) on &lt;strong&gt;non-ui components&lt;/strong&gt;: data models, data services, facades, state management, etc. And - with Angular - we can use the &lt;code&gt;TestBed&lt;/code&gt; object, provided by the Angular framework, to configure Dependency Injection (DI) and &lt;code&gt;ngModules&lt;/code&gt; within a test cycle.&lt;/p&gt;

&lt;p&gt;On the other hand, we have the harsh reality: &lt;strong&gt;UI components&lt;/strong&gt; unit testing is often problematic, painful, and provides little true ROI. It's not rare to have more lines of testing code in the UI specs files than the UI component code under test itself. But this doesn't mean we don't want to test them. They should be tested as everything else in your code, maybe not at unit level though. &lt;/p&gt;

&lt;h2&gt;
  
  
  Cypress.io and e2e testing
&lt;/h2&gt;

&lt;p&gt;We often want to test UI components to answer two basic questions:&lt;/p&gt;

&lt;p&gt;(1) Does the UI render as we expected; not just DOM structure but also the styling and layouts?&lt;br&gt;
(2) Do the workflows perform as expected?&lt;/p&gt;

&lt;p&gt;Cypress.io elegantly provides powerful, intuitive features to do these.&lt;/p&gt;

&lt;p&gt;Cypress is a great agnostic javascript testing tool that shines on e2e tests, as an alternative to Selenium (wrapped by Protractor in Angular framework). For aspect (2) above, Cypress.io simulates the user and the 'user' interactions based on custom Cypress scripts (eg steps).&lt;/p&gt;

&lt;p&gt;The most exciting aspect is that Cypress.io has brought an incredibly friendly API to the JavaScript testing world. If you're a complete newbie to Cypress.io, it probably won't take more than 10 minutes to go from scratch to having a simple working test. Its documentation is amazingly easy to read with lots of examples.&lt;/p&gt;

&lt;p&gt;If we want to use Cypress to test UI component functionality and UI, we do not need a &lt;code&gt;TestBed&lt;/code&gt;... we are testing the actual "deployed" Application (instead of only component source) and all the necessary elements to run the tests are already instantiated.&lt;/p&gt;

&lt;p&gt;Among all of e2e aspects concerning the adoption of Cypress.io for an Angular codebase, we'll focus on how we can spy or stub an Angular Component method without having a &lt;code&gt;TestBed&lt;/code&gt;, by grabbing the available component running instance from the DOM.&lt;/p&gt;
&lt;h2&gt;
  
  
  Why stub or spy?
&lt;/h2&gt;

&lt;p&gt;During unit or integration tests, you usually search for elements on the DOM, like texts, components attributes, etc, to check if the app is showing/hiding or animating anything. According to what you find on the DOM you get to the conclusion that a piece of the web app is working and the users supposedly see on the screen what you expected them to see. For example, if you find that a certain ui component has a "display: none" attribute, you should trust that it won't be visible or taking any space on the screen. You write your tests based on the trust that the browser is a piece of this puzzle that is already tested by its developers and is doing its job correctly. In other words, you're focused on &lt;em&gt;your code only&lt;/em&gt;, so you can write your tests using non-visual (functional) isolated tests.&lt;/p&gt;

&lt;p&gt;If you are concerned about how the browser is doing its job (visually) or about whether your CSS classes weren't broken by someone messing around with the design then you'll need some testing tools to compare snapshots of the current browser's rendering results with some known reference snapshots. We are aware that each browser renders things differently from each other. You can add plugins to Cypress.io to test your application design visually.&lt;/p&gt;

&lt;p&gt;But if you only want to keep track of the application workflows during an e2e testing cycle, Cypress.io can provide you enough tools out-of-the-box (no need for plugins). Sometimes, inspecting what is inside of a component is just a hard thing to do. For example, if you're using chart.js, it uses &lt;code&gt;canvas&lt;/code&gt; to draw the charts - it's not possible to check the canvas content from a functional approach. Instead of trying to inspect what is inside the &lt;code&gt;canvas&lt;/code&gt; element you'd better step back and just stick to checking, for example, if the function that generates the data for the chart was successfully called with the right arguments; or whether the event handlers that should be fired as a consequence of user interactions with the canvas elements were really called. In that case, when monitoring these function callings, you want to know three things:&lt;/p&gt;

&lt;p&gt;(a) Whether and (b) when a function is being called&lt;br&gt;
(c) Whether it's being called with the right arguments&lt;/p&gt;

&lt;p&gt;To do (a) to (c) you must get a reference to your angular component object (the instance of the component's class). For us it's important to keep in mind that we don't have the &lt;code&gt;TestBed&lt;/code&gt; available in this scenario, so we have to grab the component instance object by ourselves.&lt;/p&gt;
&lt;h2&gt;
  
  
  Let's do it
&lt;/h2&gt;

&lt;p&gt;Let's suppose you have the following &lt;code&gt;ComponentUnderTest&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;selector&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;comp-to-test&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;template&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`
    &amp;lt;button (click)="_methodToBeTested($event)"
            cy-data-button&amp;gt;
      Click me
    &amp;lt;/button&amp;gt;
  `&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;ComponentUnderTest&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// This method is fired upon user interaction&lt;/span&gt;
  &lt;span class="nx"&gt;_methodToBeTested&lt;/span&gt;&lt;span class="p"&gt;(...)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To gain access to the &lt;code&gt;methodToBeTested&lt;/code&gt; we'll use the &lt;code&gt;ng&lt;/code&gt; global object available from the global &lt;code&gt;window&lt;/code&gt; object. From that point on it's simple to do what you need.&lt;/p&gt;

&lt;p&gt;The important thing to notice is that you cannot access &lt;code&gt;window&lt;/code&gt; or &lt;code&gt;document&lt;/code&gt; directly from within your test &lt;code&gt;it()&lt;/code&gt; function. You must use the available &lt;code&gt;cy.window()&lt;/code&gt; and &lt;code&gt;cy.document()&lt;/code&gt; chainable methods to access those objects.&lt;/p&gt;

&lt;h3&gt;
  
  
  Using &lt;code&gt;cy.stub&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Your test will probably look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;describe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Trying to call some angular component method&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Should call methodToBeTested&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;angular&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="c1"&gt;// You can access the window object in cypress using&lt;/span&gt;
    &lt;span class="c1"&gt;//   window() method&lt;/span&gt;
    &lt;span class="nx"&gt;cy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;win&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Grab a reference to the global ng object&lt;/span&gt;
        &lt;span class="nx"&gt;angular&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;win&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;ng&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;})&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;cy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;componentInstance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;angular&lt;/span&gt;
             &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getComponent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;comp-to-test&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

         &lt;span class="nx"&gt;cy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stub&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;componentInstance&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;_methodToBeTested&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

         &lt;span class="nx"&gt;cy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;button[cy-data-button]&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;click&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

         &lt;span class="c1"&gt;// Just put this test to the end of the event loop&lt;/span&gt;
         &lt;span class="c1"&gt;//   in order to make sure angular runtime engine&lt;/span&gt;
         &lt;span class="c1"&gt;//   will have fired the click event that calls the&lt;/span&gt;
         &lt;span class="c1"&gt;//   method calling under test.&lt;/span&gt;
         &lt;span class="nx"&gt;cy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;wait&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; 
           &lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;componentInstance&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_methodToBeTested&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;to&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;have&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;been&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;called&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Using &lt;code&gt;cy.spy&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Now, the stub approach is fast. But it can potentially hide the side effects that clicking on that button can cause. In the end, by using Cypress, I assume that you're interested in e2e tests, and chances are that you want to observe the thorough behavior of your app. If you wish to through the real side effects caused by the method called when that button is clicked, you must use &lt;code&gt;cy.spy&lt;/code&gt; instead of &lt;code&gt;cy.stub&lt;/code&gt;. If anything fails you will be able to visually inspect what happened during the test (by the way, Cypress, by default, records all of the application visual behavior, even in headless mode, in mp4 format).&lt;/p&gt;

&lt;p&gt;So we need to do a single adjustment to replace &lt;code&gt;cy.stub&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;describe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Trying to call some angular component method&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Should call methodToBeTested&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;angular&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;cy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;win&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;angular&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;win&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;ng&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;cy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;componentInstance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;angular&lt;/span&gt;
             &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getComponent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;comp-to-test&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

         &lt;span class="c1"&gt;// Here we had cy.stub before&lt;/span&gt;
         &lt;span class="nx"&gt;cy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;spy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;componentInstance&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;_methodToBeTested&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

         &lt;span class="nx"&gt;cy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;button[cy-data-button]&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;click&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

         &lt;span class="nx"&gt;cy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;wait&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; 
           &lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;componentInstance&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_methodToBeTested&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;to&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;have&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;been&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;called&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;If you're thinking of sparing some time to learn JavaScript testing tools, it's worth it taking a look at Cypress.io. It's a short-learning-curve and still powerful tool available out there.&lt;/p&gt;

&lt;p&gt;If you find something that I left out from this article, in the scope of stubs and spies with Angular, just mention it in the comments and I'll insert it in the text.&lt;/p&gt;

&lt;p&gt;Also, I owe a special thanks to Thomas Burleson for dedicating some time to review the text and point some directions concerning the reasons that support Cypress.io integration to angular.&lt;/p&gt;

</description>
      <category>angular</category>
      <category>cypress</category>
      <category>testing</category>
    </item>
  </channel>
</rss>
