<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ankita Joshi on Medium]]></title>
        <description><![CDATA[Stories by Ankita Joshi on Medium]]></description>
        <link>https://medium.com/@ankita.joshi_64658?source=rss-4ae430e8abb------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*CA_NY2f2RWmHfVA8Yvk9Tg.jpeg</url>
            <title>Stories by Ankita Joshi on Medium</title>
            <link>https://medium.com/@ankita.joshi_64658?source=rss-4ae430e8abb------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 14:03:50 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ankita.joshi_64658/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Application Structure and Best Practices Of Angular — Part 2]]></title>
            <link>https://blog.nonstopio.com/application-structure-and-best-practices-of-angular-part-2-e00a80f5483b?source=rss-4ae430e8abb------2</link>
            <guid isPermaLink="false">https://medium.com/p/e00a80f5483b</guid>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[application-architecture]]></category>
            <category><![CDATA[angular-application]]></category>
            <category><![CDATA[angular-best-practices]]></category>
            <category><![CDATA[angular]]></category>
            <dc:creator><![CDATA[Ankita Joshi]]></dc:creator>
            <pubDate>Thu, 06 Jan 2022 10:56:16 GMT</pubDate>
            <atom:updated>2022-01-06T10:56:16.485Z</atom:updated>
            <content:encoded><![CDATA[<h3>Application Structure and Best Practices Of Angular — Part 2</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*g0GaP4R9Zoq2s_31ksfOVA.jpeg" /><figcaption>Image from <a href="https://unsplash.com/">https://unsplash.com/</a></figcaption></figure><p>This is part 2 of Angular best practices, in the <a href="https://medium.com/nonstopio/application-structure-and-best-practices-of-angular-part-1-a508e94b2545">first part</a> we learn about Angular CLI, modular structure of Angular application, lazy loading, and lifecycle hooks of angular (detailed story <a href="https://medium.com/nonstopio/angular-lifecycle-hooks-makes-angular-component-more-reliable-2ac799c81650">here</a>).</p><p>In this part, we will look into the folder structure and nomenclature, coding styles of Angular applications. For a better understanding of code, we need this thing in our life.</p><h4><strong>Folder Structure of Angular Application —</strong></h4><p>To enable us to identify, locate and use our code files in the application efficiently and effectively we need to think about naming our files consistently and structuring our files in a well-structured and unambiguous folder structure.</p><p>By default, angular provides us basic file structure as below when we run the command, which will create a new application skeleton with all new structures.</p><pre>ng new sample-project</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/310/1*K_4-Nn-ylz8Kd6n1stdfKg.png" /></figure><p>The skeleton is for a simple Welcome application that is ready to run and easy to modify.</p><p>First of all, we will see<strong> CLI configuration context</strong>, which means related to all internal configurations of the angular application.</p><p>.editorconfig— Configuration for code editors for all developers working on applications.</p><p>.gitignore— Specifies files to be intentionally untracked that git should ignore.</p><p>README.md— Introductory documentation for the application.</p><p>package.json— Provides all npm package dependencies (node modules) that are needed by the application.</p><p>package-lock.json— Provides version information for all packages installed into node_modules by the npm client. Changes over npm install command run for application.</p><p>angular.json— CLI configuration defaults application, including configuration options for versions of the application, build, serve, and test tools that the CLI uses. Which style is application using and all.</p><p>tsconfig.json— The base typescript configuration for the application.</p><p>src/— Source files for the application.</p><p>node_modules/— Provides npm packages to the application. node_modules dependencies are visible to all.</p><p><strong>Application Project Files —</strong></p><p>By default, the CLI command ng new sample-project creates an application folder named &quot;sample-project&quot; and generates a new application skeleton in a src/ folder at the top level of the application. This newly generated src/ folder contains one default component called AppComponent. Files related to AppComponent are present therein src/app/ folder. Which will contain the root module, component, and template.</p><p>We can use the ng generate command on the command line to add functionality and data to the application, like components, modules, directives, services, pipes, etc.</p><p>Subfolders of src/ contain the application source and application-specific configuration. Files at the top level of src/ known as <strong>Application source files</strong> which support testing and running your application. We will see details of files and folders in src/ (refer to image above showing the file structure of the angular application).</p><p><strong>app folder —</strong></p><p>Contains the component files in which your application logic and data are defined. Angular components, templates, and styles go in here.</p><p>app/app.module.ts — This file defines the root module, named AppModule, that tells the Angular flow of the application. Initially declares only the AppComponent. As you add more components or modules to the app, they must be declared here.</p><p>app/app.component.ts— This file defines the logic for the application&#39;s root component, named AppComponent. This component by default added by ng new sample-project, rather every new application will have AppComponent.</p><p>app/app.component.html — This file defines the HTML template associated with the root AppComponent.</p><p>app/app.component.less — This file defines the base CSS stylesheet for the root AppComponent. If you or choosing any other style then the file will be for that style. Like app.component.css.</p><p>app/app.component.spec.ts — This file defines a unit test for the root AppComponent.</p><p><strong>Assets Folder —</strong></p><p>assets/ contains images, logos, and other asset files which you want publicly available and to be copied as-is when you build your application.</p><p><strong>Environments Folder —</strong></p><p>environments/contains build configuration options environments. By default, there is an unnamed standard development environment and a production environment. You can define additional target environment configurations like staging.</p><p><strong>Favicon.ico</strong></p><p>favicon.ico is an icon to use for this angular application in the bookmark bar or browser tab.</p><p><strong>Index.html</strong></p><p>index.html is the main and first HTML page that is served when our angular application site loads.</p><p><strong>Main.ts</strong></p><p>main.ts is the main entry point of our angular application. It compiles the application with the JIT compiler and bootstraps the application&#39;s root module (here it is AppModule) to run in the browser. You can also use the AOT compiler without changing any code by appending the --aot flag to the CLI build and serve commands.</p><p><strong>polyfills.ts</strong></p><p>This file includes polyfills needed by Angular and is loaded before application. Also provides polyfill scripts for browser support.</p><p><strong>styles.less</strong></p><p>styles.less is lists CSS/LESS files that supply styles for a project. Also, we can include global style here directly using the class name.</p><p><strong>test.ts</strong></p><p>test.ts is a file required by karma.conf.js and loads recursively all the .spec and framework files. This is the main entry point for your unit tests, with some Angular-specific configuration. You don&#39;t typically need to edit this file.</p><p>All above is the default folder structure of the application provided by Angular itself. This folder structure is very good to use for all your application as this is globally used by almost all developers, so others can better understand your code.</p><p>While using this folder structure we can have our components(which will be required by our application and will be added later after generating the application) should be listed like below.</p><p>In general, we need to use the CLI command — ng generate component compoenent-name which will help us to maintain continuity in an application.</p><p>For example — In the below image we can see the logistics folder. In which we have types of logistics in a different folder. Like this, we can maintain components differently for our application. These components will list out in the root module that is AppModule. If we have many components then we can make a different module and list them in AppModule. For example, we can make LogisticsModule to define all components, services in one place for logistics with ease.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/326/1*VOpJ95qnWpki_65N07LXkQ.png" /></figure><p>Also, in the above image, we can learn nomenclature though. Let’s see how —</p><p>We can use consistent names for all features. We should follow a pattern that describes the feature then its type. The recommended pattern is feature.type.ts.</p><p>If the file feature name contains multiple words in it then do use dashes to separate words in between.</p><p>Do use conventional type names including .service, .component, .pipe, .module, and .directive.</p><p>For component, the file name should contain .component in it. And the component name should have a prefix Component like LogisticsComponent.</p><p>For service, the file name should contain .service in it. And the service name should have a prefix Service like LogisticsService. A good is to use a single service file for the whole application. Which will be useful to reuse the code. That can be placed in the root folder.</p><p>Now let’s discuss about <strong>coding style in Angular</strong> —</p><p>Our coding should be cleaner, easier to read and maintain, and more testable for better understanding and acknowledgment. To achieve this we should apply the <a href="https://en.wikipedia.org/wiki/Single-responsibility_principle"><strong>single responsibility principle</strong></a> for our coding in the Angular application.</p><p>One file should define one thing at a time that is component, service, pipe, etc.</p><p>Each file should have a limit of at least 400 lines. The key is to make the code more reusable, easier to read, and less mistake-prone.</p><p>Each function of the component should as small as possible (we can put our limit to 75 lines or so). Small functions are easier to test, especially when they do one thing and serve one purpose. Small functions are promoting reuse, easier to read, easier to maintain.</p><p>Do use <em>dashed-case</em> or <em>kebab-case</em> for naming the element selectors of components.</p><p>Conclusion — Here we have learned the basics of the coding style of angular which will help normally to build applications.</p><p>References — <a href="https://angular.io/">https://angular.io/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e00a80f5483b" width="1" height="1" alt=""><hr><p><a href="https://blog.nonstopio.com/application-structure-and-best-practices-of-angular-part-2-e00a80f5483b">Application Structure and Best Practices Of Angular — Part 2</a> was originally published in <a href="https://blog.nonstopio.com/">nonstopio</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Application Structure and Best Practices Of Angular — Part 1]]></title>
            <link>https://blog.nonstopio.com/application-structure-and-best-practices-of-angular-part-1-a508e94b2545?source=rss-4ae430e8abb------2</link>
            <guid isPermaLink="false">https://medium.com/p/a508e94b2545</guid>
            <category><![CDATA[angular-cli]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[structure]]></category>
            <category><![CDATA[angular-best-practices]]></category>
            <dc:creator><![CDATA[Ankita Joshi]]></dc:creator>
            <pubDate>Mon, 04 Oct 2021 13:27:44 GMT</pubDate>
            <atom:updated>2021-12-16T06:50:01.281Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Application Structure and Best Practices Of Angular — Part 1</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*g0GaP4R9Zoq2s_31ksfOVA.jpeg" /><figcaption>Image from <a href="https://unsplash.com/">https://unsplash.com/</a></figcaption></figure><p>Before this, we have spoken about the <a href="https://medium.com/nonstopio/angular-architecture-pattern-bc69f9c3372c">Angular Architecture Pattern</a>. Where we learn the basics about Modular Architecture or Domain-Driven Design. Now we will see how to actually design any Angular project and what are the best practices to do that.</p><p>To write any angular application we can use Angular CLI (not mandatory but keeps it simple and easy :) ).</p><h4><strong>Angular CLI -</strong></h4><p>The Angular CLI is a command-line interface tool that you use to initialize, develop, scaffold, and maintain Angular applications directly from a command shell.</p><p>Install the CLI using the npm package manager:</p><blockquote>npm install -g @angular/cli</blockquote><p>Let’s start with a new, basic Angular project, to create, build, and serve it on a DevServer, use the following commands</p><blockquote>ng new basic-project</blockquote><blockquote>cd basic-project</blockquote><blockquote>ng serve</blockquote><p>Let’s see some basic CLI commands to work on the angular application directory easily. CLI will create all directories symmetrically. If we are creating the components using CLI then all components directories will have the same format of file naming and will contain the same files (component, template, style, etc).</p><ol><li><strong>ng-add —</strong></li></ol><p>Adds the npm package for a published library to your workspace</p><p>example — <strong>ng add @angular/material</strong></p><p><strong>2. ng-build (ng-b) —</strong></p><p>Compiles an Angular app into an output directory named dist/ at the given output path. Must be executed from within a workspace directory.</p><p><strong>3. ng generate (ng-g) —</strong></p><p>Generates and/or modifies files based on a schematic of the application or module. Basically used to generate components, directives, pipes, modules, services, routes, etc</p><p>uses — <strong><em>ng generate component basic-project-first-component</em></strong><em> </em>this command will create a component directory of name BasicProjectFirstComponent. It will create files for component, template, style and test.</p><p>In this way, we can create</p><p>directive — ng generate directive name_of_directive</p><p>service — ng generate service name_of_service</p><p>module — ng generate module name_of_module</p><p>pipe — ng generate pipe name_of_pipe</p><p><strong>4. ng-lint —</strong></p><p>Runs linting tools on Angular app code in a given project folder.</p><p><strong>5. ng-serve —</strong></p><p>Builds and serves your app, continue rebuilding on changes.</p><h4><strong>Modular Structure of Angular Application —</strong></h4><p>Here comes a domain-driven design strategy in the picture (we can say). We can divide our application into modules according to our domains (example of shipping application — login, delivery, infra, packages, receipt). Here each module contains components related to a domain, then services, interfaces, and many more. Which will separate the business logic of the application for easy use. <br>Modules or components can be reusable at different places in the application.</p><p><strong>Use of shared module —</strong></p><p>This module can contain all frequently reusable components, directives, pipes, etc for application.</p><p>Always create a feature module named SharedModule in a shared folder, for example, app/shared/shared.module.ts defines SharedModule.</p><h4>Lazy Loaded Folders —</h4><p>Arranging folders or features in lazy loading is one of the best ways to achieve the best practice of Angular. It will help to structure your application.</p><p>A distinct application feature may be lazy loaded or loaded on demand not on when the application starts.</p><p>A typical lazy loaded folder contains a routing component, its child components, and their related assets and modules. The folder makes it easy to identify lazy loaded feature.</p><h4>Lifecycle Hooks —</h4><p>We can use Lifecycle hooks to tap into important events exposed by Angular, like ngOnInit, NgOnChanges, ngDoCheck, etc. Implementation of the lifecycle hook interfaces prescribes typed method signatures. We can use those signatures to flag spelling and syntax mistakes.</p><p>About lifecycle hooks, we can learn here — <a href="https://medium.com/nonstopio/angular-lifecycle-hooks-makes-angular-component-more-reliable-2ac799c81650">https://medium.com/nonstopio/angular-lifecycle-hooks-makes-angular-component-more-reliable-2ac799c81650</a></p><p>In the next story, we will look into the file and folder structure of the angular application and nomenclature in angular.</p><p>Reference — <a href="https://angular.io/guide">https://angular.io/guide</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a508e94b2545" width="1" height="1" alt=""><hr><p><a href="https://blog.nonstopio.com/application-structure-and-best-practices-of-angular-part-1-a508e94b2545">Application Structure and Best Practices Of Angular — Part 1</a> was originally published in <a href="https://blog.nonstopio.com/">nonstopio</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Intro to Angular Directives]]></title>
            <link>https://blog.nonstopio.com/intro-to-angular-directives-abaea97d6da1?source=rss-4ae430e8abb------2</link>
            <guid isPermaLink="false">https://medium.com/p/abaea97d6da1</guid>
            <category><![CDATA[html]]></category>
            <category><![CDATA[directives]]></category>
            <category><![CDATA[web-app-development]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[custom-directive]]></category>
            <dc:creator><![CDATA[Ankita Joshi]]></dc:creator>
            <pubDate>Tue, 27 Jul 2021 03:10:41 GMT</pubDate>
            <atom:updated>2021-07-27T03:10:41.051Z</atom:updated>
            <content:encoded><![CDATA[<p>As we all know, angular is made up of three basic concepts in components i.e class, template (Dom elements), and style. Here we are talking about Angular Directives. Directives play a major role to bind components together. Directives are classes that add additional behavior to elements in your Angular applications. With Angular’s directives, you can manage forms, lists, styles, and what users see.</p><p>Let’s discuss the simplest and easiest way to use directives in your application.</p><p>Directives have three different types -</p><ol><li>Attribute Directives — which changes the appearance and behavior of components, elements</li><li>Structural Directives — which change the DOM layout with elements</li><li>Component Directives — which can say, directive with the template.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/0*BssxK6gHtRqjDb9B" /><figcaption>Types Of Directives</figcaption></figure><p><strong>Attribute Directives</strong></p><p>With attribute directives, you can change the appearance or behavior of DOM and components, attributes, properties. Attribute directives can also change the behavior of another directive. In my observation attribute directive mostly use to change the style of elements.</p><p>We often use the most common attribute directives in our daily coding like -</p><ul><li>NgClass — adds and removes a set of CSS classes.</li><li>NgStyle — adds and removes a set of HTML styles.</li><li>NgModel — adds two-way data binding to an HTML form element.</li></ul><blockquote><em>For the creation of any type of directive, we can use angular CLI —</em></blockquote><blockquote><strong><em>ng generate directive directive_name</em></strong></blockquote><p>We are creating a simple Attribute Directive — <strong>HeaderStyleDirective</strong>, which will be used to style any header on the page. Using this we can change text font size, font color, etc.</p><pre>import { Directive, ElementRef } from ‘@angular/core’;<br></pre><pre>@Directive({</pre><pre>selector: ‘[headerStyle]’</pre><pre>})<br></pre><pre>export class HeaderStyleDirective {</pre><pre>constructor(elRef: ElementRef) {</pre><pre>elRef.nativeElement.style.color = ‘#00abff’;</pre><pre>elRef.nativeElement.style.fontSize = ‘22px’;</pre><pre>}</pre><pre>}</pre><ol><li>The @Directive() decorator’s configuration property specifies the directive’s CSS attribute selector, [headerStyle]. Which will be used as an attribute in the element.</li><li>Importing ElementRef from @angular/core. which will grant direct access to the host DOM element through its nativeElement property.</li><li>Add ElementRef in the directive’s constructor() to inject a reference to the host DOM element, the element to which you apply headerStyle.</li><li>Add logic to the HeaderStyleDirective a class that sets the font property of header text.</li></ol><p>This directive can be used as —</p><pre>&lt;span headerStyle&gt;This is a Header..&lt;/span&gt;</pre><p><strong>Structural Directives</strong></p><p>Structural directives are responsible for HTML layout. They restructuring the DOM’s structure, typically by adding, removing, and manipulating the host elements.</p><p>Below are some built-in structural directives available —</p><ul><li>NgIf — conditionally adds or removes subviews from the template.</li><li>NgFor — repeat a node for each item in a list.</li><li>NgSwitch — a set of directives that switch among subsequent views.</li></ul><p>We will see NgIf example for a structural directive —</p><pre>&lt;app-record-screen *ngIf=”isAdded” [record]=”record”&gt;&lt;/app-record-screen&gt;</pre><p>In the above code section, we can see an HTML element in Angular which have ngIf applied. If isAdded the condition is true, then the above section of AppRecordScreen will execute in DOM else ngIf will remove the AppRecordScreen from the DOM and dispose of the component and all of its sub-components.</p><p><strong>Component Directive</strong></p><p>Here we can say every component which we are using with a CSS selector is directive itself. Component directives always build with a template in it that adds it in another HTML DOM. These directives use a decorator @Component in its metadata.</p><p>Reference — <a href="https://angular.io/guide/attribute-directives">Angular.io</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=abaea97d6da1" width="1" height="1" alt=""><hr><p><a href="https://blog.nonstopio.com/intro-to-angular-directives-abaea97d6da1">Intro to Angular Directives</a> was originally published in <a href="https://blog.nonstopio.com/">nonstopio</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Angular Lifecycle Hooks makes angular component more reliable]]></title>
            <link>https://blog.nonstopio.com/angular-lifecycle-hooks-makes-angular-component-more-reliable-2ac799c81650?source=rss-4ae430e8abb------2</link>
            <guid isPermaLink="false">https://medium.com/p/2ac799c81650</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[lifecycle-hook]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[angular-components]]></category>
            <dc:creator><![CDATA[Ankita Joshi]]></dc:creator>
            <pubDate>Sun, 23 May 2021 13:35:14 GMT</pubDate>
            <atom:updated>2021-05-23T13:35:14.446Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*NvAdvVRg85oIO-okKwqGOw.png" /><figcaption><a href="https://www.google.com/imgres?imgurl=https%3A%2F%2Fres.cloudinary.com%2Fpracticaldev%2Fimage%2Ffetch%2Fs--ZbARfhne--%2Fc_imagga_scale%2Cf_auto%2Cfl_progressive%2Ch_420%2Cq_auto%2Cw_1000%2Fhttps%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fd2btt7kbfelqimb3p2rc.png&amp;imgrefurl=https%3A%2F%2Fdev.to%2Fdev_coder%2Fangular-8-lifecycle-hooks-14i0&amp;tbnid=7AH4Nn_nTZEnQM&amp;vet=12ahUKEwiNwszEu9vwAhWGSCsKHfuiBU8QMyhFegQIARBl..i&amp;docid=aRXh3EJvaquhDM&amp;w=1000&amp;h=420&amp;q=lifecycle%20hooks%20angular&amp;ved=2ahUKEwiNwszEu9vwAhWGSCsKHfuiBU8QMyhFegQIARBl">google</a></figcaption></figure><p><strong>Concept of lifecycle hooks </strong>-</p><p>To run any angular component it needs to go through one cycle of events. This lifecycle of events called Angular Lifecycle Hooks. Which hooks up each data flow of the component. This lifecycle starts from the initialization of components and ends with the destruction of components.</p><p>Every angular component has its lifecycle. A lifecycle has typical eight phases called lifecycle hooks. So we have total of eight lifecycle hooks in angular component or directive</p><p><strong>What is lifecycle hooks exactly -</strong></p><p>A developer can call any lifecycle hook event by calling its function at that phase of the component lifecycle.</p><p>The exact use of angular lifecycle hooks — Developers can make use of a life cycle by implementing one or more hook functions from the Angular core library. Hook events can be used at any stage of an application to get excellent control over the components.</p><blockquote>Note — These lifecycle hooks apply to both components and directives.</blockquote><p><strong>A quick look at all 8 lifecycle hooks -</strong></p><p>Below images are the list of lifecycle hooks with a quick explanation. Among 8 lifecycle hooks first three are parent components lifecycle hooks, the next four are of child’s components lifecycle hooks and the last one is the last lifecycle hook of a component.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/629/1*Jd-8d-Mf1r0tGu3ADx8-MQ.png" /><figcaption>Parent Component Life cycle Hooks</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/626/1*4e9A8n0rtVoPb47AokMb1A.png" /><figcaption>Child Component Life cycle Hooks</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/628/1*INrQA-f7KZp8oWnHc0V8hQ.png" /><figcaption>Parent Components last Life cycle Hooks</figcaption></figure><p><strong>Use of Constructor in angular application-</strong></p><p>A constructor is a special method that will be called whenever we create new objects. It is a feature of the class(typescript) itself, an object-oriented design concept. A constructor is also used to initialize the class members.</p><pre>@Component({</pre><pre>//…</pre><pre>})</pre><pre>export class ExampleComponent {</pre><pre>constructor(){}</pre><pre>}</pre><p>We must have to use the constructor to inject dependencies in the angular component. Angular always executes the constructor method first then all lifecycle hooks will come into the picture.</p><p><strong>Now we will look into lifecycle hooks -</strong></p><ol><li><strong>ngOnChanges() -</strong></li></ol><p>when any data-bound property of a directive or component changes, the OnChanges lifecycle hook is called. Let&#39;s have a look into example -</p><pre>@Component({</pre><pre>selector: ‘example’,</pre><pre>template: `</pre><pre>&lt;div *ngFor=”let customer of customers”&gt;</pre><pre>{{customer}}</pre><pre>&lt;/div&gt;</pre><pre>`</pre><pre>})</pre><pre>export class ExampleComponent implements OnChanges {</pre><pre>@Input() customers;</pre><pre>ngOnChanges() {</pre><pre>console.log(“The property of customer changed”);</pre><pre>}</pre><pre>}</pre><pre>@Component({</pre><pre>selector: ‘example1’,</pre><pre>template: `</pre><pre>&lt;example [customers]=”customers”&gt;&lt;/example&gt;</pre><pre>`</pre><pre>})</pre><pre>export class Example1Component {</pre><pre>customers = [“Customer1”];</pre><pre>}</pre><p>Here we have used interface OnChanges on ExampleComponent and called method ngOnChanges() which will call the OnChanges lifecycle hook. Example1Component is binding its customers property to ExampleComponent. On change of customers property from Example1Component, ngOnChanges() method of ExampleComponent will call. In short, every change of @input value will call ngOnChanges() lifecycle hook.</p><p><strong>2. ngOnInit() -</strong></p><p>This lifecycle hook is the most usable hook as we can initialize all methods and variables here in it. When we need to initialize any method where we can have the logic of getting initial data for a component then we can use ngOnInit() to call that method. This lifecycle hook will implement after all data bound properties to get called so we will have all properties to use initially. Basically, this lifecycle hook will call once and after ngOnChanges() called initially.</p><pre>@Component({</pre><pre>Selector: ‘example’</pre><pre>})</pre><pre>export class ExampleComponent implements OnInit {</pre><pre>ngOnInit() {</pre><pre>console.log(“This is OnInit lifecycle hook”);</pre><pre>}</pre><pre>}</pre><blockquote>As we have a constructor to initialize everything then also we are calling ngOnInit, let me explain the difference between constructor and ngOnInit here.</blockquote><p><strong>Difference between constructor and ngOnInit here -</strong></p><p>The Constructor is executed when the class is instantiated as a default method of the class and ensures all initialization of fields in the class. Angular, or better Dependency Injector (DI), analyses the constructor parameters and when it creates a new instance by calling a new class() it tries to find providers that match the types of the constructor parameters, resolves them, and passes them to the constructor. ngOnInit is a life cycle hook called by Angular after data bound is done for the first time and angular is done creating the component. Implementing OnInit is not mandatory but considered good practice.</p><pre>export class ExampleComponent implements OnInit {</pre><pre>constructor() {</pre><pre>// Called first time before the ngOnInit()</pre><pre>}</pre><pre>ngOnInit() {</pre><pre>// Called after the constructor and called after the first  <br>// ngOnChanges() </pre><pre>}</pre><pre>}</pre><p>Mostly we use ngOnInit for all the initialization or declaration and avoid doing work in the constructor. The constructor should only be used to initialize class members but shouldn’t perform actual work.</p><p>So you should use constructor() to set up Dependency Injection. ngOnInit() is a better place to “start” — it’s where the component&#39;s bindings are resolved.</p><p><strong>3. ngDoCheck() -</strong></p><p>This lifecycle hook comes after ngOnInit() and basically calls to detect and act upon changes that Angular can’t or won’t detect on its own. DoCheck invokes a custom change-detection function for a directive, in addition to the check performed by the default change-detector.</p><pre>export class ExampleComponent implements OnChanges, DoCheck {</pre><pre>@Input() customers;</pre><pre>ngOnChanges() {</pre><pre>console.log(&quot;The customers property has been changed&quot;);</pre><pre>}</pre><pre>ngDoCheck() {</pre><pre>console.log(&quot;ExampleCompoent&#39;s ngDoCheck called.&quot;);</pre><pre>}</pre><pre>}</pre><p><strong>4. ngAfterContentInit()</strong></p><p>ngAfterContentInit() is called when the content of a component has initialized. In this case, content is the component that are within the &lt;ng-content&gt;&lt;/ng-content&gt; tags. After ngDoCheck() it is called initially.</p><pre>@Component({</pre><pre>selector: &#39;example2&#39;,</pre><pre>template: `</pre><pre>&lt;div&gt;This is a Example2 Componnet&lt;/div&gt;</pre><pre>`</pre><pre>})</pre><pre>export class Example2Component {}</pre><pre>@Component({</pre><pre>selector: example1,</pre><pre>template: `</pre><pre>&lt;ng-content&gt;&lt;/ng-content&gt;</pre><pre>`</pre><pre>})</pre><pre>export class Example1Component implements AfterContentInit {</pre><pre>ngAfterContentInit() {</pre><pre>}</pre><pre>}</pre><pre>@Component({</pre><pre>template: `</pre><pre>&lt;example1&gt;</pre><pre>&lt;example2&gt;&lt;/example2&gt;</pre><pre>&lt;/example1&gt;</pre><pre>`</pre><pre>})</pre><pre>export class ExampleComponent {}</pre><p>The Example1Component will have any elements in between its tag &lt;example1&gt; &lt;/example1&gt; inside the ng-content tag. Now, in the Examplecomponent, the Example2Component is projected in the example1 tag. When the Example2Component is being initialized, the ngAfterContentInit hook will be called in Example1Component.</p><p><strong>5. ngAfterContentChecked()</strong></p><p>This lifecycle hook performs its work by knowing the change in the content of the component using Angular change detection. It gets its call after ngAftercontentInit() and also gets executed after every execution od ngDoCheck().</p><pre>@Component({</pre><pre>selector: &#39;example1&#39;,</pre><pre>template: `</pre><pre>&lt;ng-content&gt;&lt;/ng-content&gt;</pre><pre>`</pre><pre>})</pre><pre>export class Example1Component implements AfterContentInit {</pre><pre>ngAfterContentInit() {</pre><pre>}</pre><pre>}</pre><pre>@Component({</pre><pre>template: `</pre><pre>&lt;example1&gt; {{data}} &lt;/example1&gt;</pre><pre>`</pre><pre>})</pre><pre>export class ExampleComponent implements AfterContentChecked {</pre><pre>data: any</pre><pre>ngAfterContentChecked() {</pre><pre>}</pre><pre>}</pre><p>The ExampleComponent component has a data property that is inside the Example1Component. When the data property changes, the ngAfterContentChecked() method will be called.</p><p><strong>6. ngAfterViewInit()</strong></p><p>This lifecycle hook gets called after ngAfterContentChecked(). And also gets called after a component’s view and its children’s views have been created and fully initialized. This lifecycle hook will be useful when we want to reference a component instance in our component using ViewChild/ViewChildren. After the initialization of view, it gets its call only once.</p><pre>@Component({</pre><pre>selector: example1,</pre><pre>template: `</pre><pre>`</pre><pre>})</pre><pre>export class Example1Component {</pre><pre>example1Method() { }</pre><pre>}</pre><pre>@Component({</pre><pre>template: `</pre><pre>&lt;example1 #example1&gt;&lt;/example1&gt;</pre><pre>`</pre><pre>})</pre><pre>export class ExampleComponent implements AfterViewInit{</pre><pre>@ViewChild(example1) example1: Example1Component;</pre><pre>constructor() { }</pre><pre>ngAfterViewInit(){</pre><pre>this.example1.example1Method();</pre><pre>}</pre><pre>}</pre><p>We got the reference of example1’s class Example1Component by putting # in the ExampleComponent template. Then, we used ViewChild to tell Angular to set the instance of Example1Component to example1 variable.</p><p><strong>7. ngAfterViewChecked()</strong></p><p>This Angular lifecycle method gets called subsequently as it checks the component’s view and child view. This method gets called after ngAfterViewInit() and then for every ngAfterContentChecked() method. After checking and initialization are done, this lifecycle hook gets called.</p><p><strong>8. ngOnDestroy()</strong></p><p>ngOnDestroy() is a lifecycle hook that is called when a directive, pipe, or service is destroyed. This lifecycle hook is used for any custom cleanup that needs to occur when the instance is destroyed, by doing so you can prevent memory leakage. This lifecycle hook is mostly used to unsubscribe from observable streams and detach event handlers to avoid memory leaks.</p><p><strong>Conclusion -</strong></p><p>This total write-up is to describe the lifecycle of angular components and what events occur during that lifecycle and how angular handles them using Angular Lifecycle Hooks. Here we have a total of 8 lifecycle hooks that describe the start-to-end lifecycle of any component.</p><p><strong>Reference</strong> -</p><p><a href="https://angular.io/guide/lifecycle-hooks">https://angular.io/guide/lifecycle-hooks</a></p><p><a href="https://blog.logrocket.com/angular-lifecycle-hooks/">https://blog.logrocket.com/angular-lifecycle-hooks/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2ac799c81650" width="1" height="1" alt=""><hr><p><a href="https://blog.nonstopio.com/angular-lifecycle-hooks-makes-angular-component-more-reliable-2ac799c81650">Angular Lifecycle Hooks makes angular component more reliable</a> was originally published in <a href="https://blog.nonstopio.com/">nonstopio</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Angular Architecture Pattern]]></title>
            <link>https://blog.nonstopio.com/angular-architecture-pattern-bc69f9c3372c?source=rss-4ae430e8abb------2</link>
            <guid isPermaLink="false">https://medium.com/p/bc69f9c3372c</guid>
            <category><![CDATA[domain-driven-design]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[web-app-development]]></category>
            <category><![CDATA[software-architecture]]></category>
            <category><![CDATA[angular]]></category>
            <dc:creator><![CDATA[Ankita Joshi]]></dc:creator>
            <pubDate>Fri, 07 May 2021 06:38:14 GMT</pubDate>
            <atom:updated>2021-05-07T06:38:14.341Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/850/1*r3bki6062tG2XhOiuM9-1Q.jpeg" /><figcaption>Image source — <a href="https://archeyes.com/art/">Archeyes</a></figcaption></figure><p>While working with front-end applications we need a more reliable architecture, that is both maintainable and scalable. In modern front-end applications at the current stage, my preference is domain-driven architecture or design.</p><p><strong>Software architecture — </strong>Whenever we discuss any architecture and patterns we need to understand software architecture first. Software architecture exposes the structure of a system while hiding the implementation details. Each structure consists of software elements, relations among them, and properties of both elements and relations. These structures can have a considerable impact on the quality, maintainability and performance, and overall success of the project.</p><p><strong>Domain-Driven Design — </strong>Domain-driven is about designing software based on models of the underlying domain. The domain (business logic perceptive) is the center of this project while designing. For example, if a project is about travel business management, our project should have class names like hotelBooking, travelBooking, and searchForFlight.</p><p>Advantages of DDD — We will talk about more effective advantages of DDD.</p><ol><li>Here the first advantage is communication, a common nomenclature (it will have names regarding domain) is used by the team to write a code. It is easy to know terms and definitions by reading names.</li><li>Flexibility — DDD is based on object-oriented programming. So everything from the project will be an object and hence encapsulated and modular.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*B8MO_jmfy4Vdrg6BSObsRg.png" /></figure><p>Looking into the above structure we can think about angular architecture. Angular architecture is defined in main three layers is Core layer, Abstraction layer, and Presentation layer. This is also called <strong>High-Level Architecture</strong>. Here I am explaining it in very simple words. The above image refers to high-level architecture. In which we can see how major three layers of application can describe.</p><p><strong>Presentation Layer — </strong>This layer is mainly responsible for the design and user interface. Here we are defining all our angular components. It is displaying all templates and handling user events. Presentation layer also responsible for Unidirectional data flow.</p><p>Unidirectional data flow — In angular, we have a concept of the hierarchical tree structure. It means we have parents and children concept for components. They are connected via parent and children relationship. Here data flow between each component with events (actions). The child component will send action using @output and every component will accept it using @input.</p><p><strong>Abstraction layer — </strong>The abstraction layer decouples the presentation layer from the core layer. This layer will behave as a mediator and the facade for the presentation layer. It means that it will expose an API and coordinate the communication between presentation layer components and the core of the application. For example, here we call services to have data communication between template and core layer.</p><p><strong>Core layer — </strong>This layer is the innermost layer of an application, though all outside communication happens here. In this layer, one can place all state management, core modules, and services. The core layer is responsible for encapsulating the state and behavior of an application. In angular, we have services to write all API calls which contain @Injectable to inject APIs to communicate with outside. All services lie in the core layer. In this layer, we could also place any validators, mappers, or more advanced use-cases that require manipulating many slices of our UI state.</p><p><strong>Conclusion — </strong>This story gives foundational know-how of Angular project architecture which hopefully will help you build a project in minimum time, and will also create fewer complexities in the project building process.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bc69f9c3372c" width="1" height="1" alt=""><hr><p><a href="https://blog.nonstopio.com/angular-architecture-pattern-bc69f9c3372c">Angular Architecture Pattern</a> was originally published in <a href="https://blog.nonstopio.com/">nonstopio</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>