{"id":1789,"date":"2021-04-10T10:13:00","date_gmt":"2021-04-10T10:13:00","guid":{"rendered":"https:\/\/gauravw.com\/blog\/?p=1789"},"modified":"2021-04-10T10:13:06","modified_gmt":"2021-04-10T10:13:06","slug":"angular-2","status":"publish","type":"post","link":"https:\/\/gauravw.com\/blog\/2021\/04\/angular-2\/","title":{"rendered":"Angular 2+"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Html \u2013 JS Dev vs Angular Dev<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Earlier we would keep these separate. We will manage the DOM via JS. So on click of a button, we will invoke a particular JS \u2013which might validate form inputs.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Angular works in component based approach, we think in terms of components. So page is divided into components and each component&nbsp; can have other components inside it. The main component is the root-component<strong>.<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In angular, we will define form as one component and all the JS is encapsulated inside it. So the components are self-sufficient.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>How does angular start?<\/strong><br>The index.html might contain certain elements &lt;root-component&gt; etc. The angular read the file main.ts [the first typescript to be called]. In main.ts we bootstrap the root component\u2019s module. The root component\u2019s module declares a root-component. The root-component\u2019s ts file mentions its selector. So angular understands its selector as the element.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Node.js<\/strong> \u2013 runtime environment made on Chrome\u2019s V8 JS engine. IT can interpret the code written in angular or other JS frameworks.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Test = <strong>node<\/strong> -v<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>NPM<\/strong> &#8211; node package manager manages versions of angular modules . It works on package.json which contains the list of dependencies and libraries needed by the project. Similar to pom.xml maven for java, however <strong>npm<\/strong> works on git only.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">NPM gets installed with Node installation<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">After NPM installs we install CLI(ng) from NPM<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Npm install @angular\/cli&nbsp; &nbsp; [@group\/package<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Test =&nbsp; <strong>ng<\/strong> -v<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Creation of new project&nbsp;<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>ng new &lt;project-name&gt;<\/strong> \u2013 this creates new files for the project and dependencies for the needed by it using NPM. It will create bare-bones project with some files.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Angular bootstraps <strong>app-root<\/strong> component which needs to be cleaned\/ or a new component created in its place.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Creation of new Component<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>ng generate component &lt;component-name&gt;&nbsp;<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Angular Component contains<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Html &#8211; view<\/li><li>Css &#8211; view<\/li><li>Ts \u2013 backing logic<\/li><li>Spec.ts \u2013 unit testing<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Decorators <\/strong>&#8211; Everything that begins with @.&nbsp; @Component, @NgModule, @Injectable<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To declare a .ts&nbsp; as a component<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">@<strong>Component<\/strong> {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>selector<\/strong> :&nbsp; \u201capp-root\u201d \/\/ the tag for this component<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>templateUrl<\/strong> : \u201c.\/app.html\u201d \/\/ relative path<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>styleUrls<\/strong>: \u201c.\/app.css\u201d \/\/ relative path<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When you add a component, it creates 4 files with a folder and changes module.ts file.<br>In a component &#8211; <strong>template <\/strong>or <strong>templateUrl <\/strong>is mandatory.<br><strong>selector<\/strong> :&nbsp; \u201capp-root\u201d &#8211; allows to be used &lt;app-root&gt;&lt;\/app-root&gt;<br><strong>selector<\/strong> :&nbsp; [app-root] &#8211; allows to be used &lt;div app-root&gt;&lt;\/div&gt;<br><strong>selector<\/strong> :&nbsp; .app-root &#8211; allows to be used &lt;div class=\u201dapp-root\u201d&gt;&lt;\/div&gt;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><br><strong>styleUrls <\/strong>can take multiple css files or one can give it inline with<strong> styles<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>DATA Binding<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>One way binding \/ a. String Interpolation &#8211; <\/strong>{{ }}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">ts file -&gt; html<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When the value of the variable changes in .ts\/data file, it is reflected in view.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>One way binding \/ b. Property Binding &#8211; <\/strong>[]<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">ts file -&gt; html<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Used to set properties in ts file to control dom elements in view.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&lt;button [disabled]=\u201disDisabled\u201d&gt;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">isDisabled should be a boolean in ts file.<br><br>String interpolation to be used when you want to display a text, property binding when you want to control element behavior using ts attribute.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You cannot mix both<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>One way binding \/ c. <\/strong><strong>Event binding<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The &lt;button click=\u201dfunct()\u201d &gt; text &lt;\/button&gt; behaves as browser click.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The &lt;button (click)=\u201dfunct()\u201d &gt; text &lt;\/button&gt; behaves as angular click and it will find the function in your component.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">One can also send funct( $event ).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Two way binding &#8211; ngModel<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In case we want to update value of variable two ways -i.e data in ts file and UI. It uses the syntax of banana in a box [(ngModel)] = \u201cvariableName\u201d<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To use this, we need to import FormModule from angular<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Aysnc refresh a value&nbsp;<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use JS API &#8211; setInterval (&nbsp; function(), interval&nbsp; )<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">setInterval ( () -&gt;&nbsp; this.date = new Date().toDateString() , 1000 )&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This ensures that the value date variable changes every second which is defined in the component.<br>{{ myFunc(abc, 2) }} \u2013 it&nbsp; can also call functions and display its values.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Directives <\/strong>&#8211; Instructions for the DOM<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Structural Directives &#8211; <\/strong>which have * before them.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>*ngIf<\/strong> \u2013 if condition for JS<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&lt;div *ngIf = \u201citems.length&gt;2\u201d&gt; &lt;\/div&gt;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>*ngFor<\/strong> \u2013 to loop over elements\/ arrays in JS.<br>example &lt;p *ngFor=\u201clet item of items\u201d &gt; {{item.element}}&nbsp; &lt;\/P&gt;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The star * is only to indicate the angular interpreter that it needs to transform this into a code that uses string interpolation, property binding which angular understands.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The *ngIf block converts to ng-template block with [ngIf] i.e property binding.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Attribute Directives<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>ngStyle <\/strong>which is a attribute directive used as a property binding. It can help to give a dynamic style to an attribute.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&lt;p [ngStyle]=\u201dbackgroundColor: myFunc()\u201d&gt;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>ngClass <\/strong>&#8211; similar to above but itnstead of adding css styles, it adds css classes dynamically.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&lt;p [ngClass]=\u201dmy-class: myFunc()\u201d&gt;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Intercommunication between components<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>Parent to child communication<\/strong><\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Giving inputs to the components<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&lt;app-image src=\u201d.\/svg\/icon.gif\u201d &gt; &lt;\/app-image&gt;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the image-component.ts<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">@Component {<br>selector :&nbsp; \u201capp-root\u201d \/\/ the tag for this component<br>templateUrl : \u201c.\/app.html\u201d \/\/ relative path<br>styleUrls: \u201c.\/app.css\u201d \/\/ relative path<br>}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">export class image {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">@Input(\u2018src\u2019) filePath : string;&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u2026&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u2018src\u2019 is an alias &#8211; optional.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Input values are populated in ngOnIt() which is called post constructor.. If you try to read the value in constructor, it won&#8217;t fetch anything,<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The above method simply works for string. In case we want to pass an object to a component?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We will change the syntax to<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&lt;app-image <strong>[src]=\u201dmyObj\u201d <\/strong>&gt; &lt;\/app-image&gt;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here, we have used src in square bracket &#8211; property binding, this tell angular to take myObj as an object reference and not as a string.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>b. Child to Parent Communication<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>&nbsp;Giving output \/ Emitting event from component<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">in Child component<br>@Output(\u2018eventNameAlias\u2019) someEventName: new EventEmitter&lt;MyObj&gt;;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">void fun () {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">someEventName.emit();<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In Parent\u2019s html<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&lt;child-component (\u2018eventNameAlias\u2019) = \u201cparentsFunction($event)\u201d &gt;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Attribute vs Structural Directive<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Attribute Directive &#8211; change or affect the element on which they are user<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Structural Directive &#8211;&nbsp; They begin with * for demarcating. Affect the whole DOM.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Making one&#8217;s own directive &#8211; ng generate directive &lt;name&gt;<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">a. using just ElementRef approach&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">@Directive({<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;selector: &#8216;[appBasicHighlight]&#8217;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">})<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">export class BasicHighlightDirective implements OnInit {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;constructor(private elementRef: ElementRef) {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;ngOnInit() {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">this.elementRef.nativeElement.style.backgroundColor = &#8216;green&#8217;;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is not considered good to change the elements directly. The DOM should be changed by angular&#8217;s API.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Because some of these styles may not be available in web services etc.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">b. Using Renderer2 and ElementRef<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;constructor(private elRef: ElementRef, private renderer: Renderer2) { }<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;ngOnInit() {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">this.renderer.setStyle(this.elRef.nativeElement, &#8216;background-color&#8217;, &#8216;blue&#8217;);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;We are using Angular API&#8217;s to change DOM<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">c. Using @HostBinding and @HostListener<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">@HostBinding &#8211; simply binds\/sets the value to the DOM<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">@HostListener &#8211; reacts to the known events in angular and sets the value in DOM<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">@HostBinding(&#8216;style.backgroundColor&#8217;) backgroundColor: string;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">@HostListener(&#8216;mouseenter&#8217;) mouseover(eventData: Event) {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;this.backgroundColor = this.highlightColor;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">backgroundColor, highlightColor can be set using @input \/ property binding.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>View Encapsulation<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This is&nbsp; Angular&#8217;s way of emulating the Shadom DOM. In this, a style if applied to a component remains local to it.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There are three options<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">@Component({<br>&nbsp; selector:<br>&nbsp; templateUrl:<br>&nbsp; styles: &#8216;<br>&nbsp; encapsulation: ViewEncapsulation.None<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">})<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>ViewEncapsulation.None<\/strong> &#8211; No Shadow DOM at all. Global styles<\/li><li><strong>ViewEncapsulation.Emulated<\/strong> &#8211; No Shadow DOM but style encapsulation emulation.<\/li><li><strong>ViewEncapsulation.Native<\/strong> &#8211; Native Shadow DOM with all it\u2019s goodness.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Note &#8211; not all browsers support native dom.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Local reference<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">a. One can refer the Dom element with #&lt;name&gt;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&lt;div class=&#8221;col-md-9&#8243;&gt;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;input type=&#8221;text&#8221; class=&#8221;form-control&#8221; trim #giftName \/&gt;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&lt;\/div&gt;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&lt;div class=&#8221;row&#8221;&gt;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;button class=&#8221;btn&#8221; (click)=&#8221;onAddGift(giftName)&#8221;&gt;Add Gift&lt;\/button&gt;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&lt;\/div&gt;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">onAddGift(giftName: HTMLInputElement) {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.userGiftName = giftName.value;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">b. <strong>@ViewChild<\/strong> &#8211; make an element in ts file of type <strong>ElementRef<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&lt;div class=&#8221;col-md-9&#8243;&gt;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;input type=&#8221;text&#8221; class=&#8221;form-control&#8221; trim #nickName \/&gt;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&lt;\/div&gt;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&lt;div class=&#8221;row&#8221;&gt;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;button class=&#8221;btn&#8221; (click)=&#8221;onAddGift(nickName)&#8221;&gt;Add Gift&lt;\/button&gt;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&lt;\/div&gt;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">onAddGift(nickName: HTMLInputElement) {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ..<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.userNickName = nickName.nativeElement.value;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">@ViewChild(&#8216;nickName&#8217;) nickName : ElementRef;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Content Projection<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To write the content between the components.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&lt;myComponent&gt;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&lt;p&gt; hii&#8230; this would be ignored&lt;\/p&gt;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&lt;\/myComponent&gt;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To make sure it comes change the mycomponents templates with &lt;ng-content&gt;&lt;\/ng-content&gt;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This will place the content between the braces at that location where the tag is present.One cna also enable &lt;ng-content select=&#8221;div&#8221;&gt; to take up only div elements.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Styling \u2013 Local and Global<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Component.css can have common style names as of other components. The angular can handle it. So component 1 can have style as \u201cheader\u201d with some css properties and component 2 can also have \u201cheader\u201d style with other properties. They will not interfere.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In order to have global styles, application specific styles \u2013 use styles.css in src folder.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Modules<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Are basically a collection of components, services.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>ng generate module &lt;name&gt;&nbsp; -&gt; creates a folder with name and name.module.ts file<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To generate components inside this module<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">ng generate component MyModule\/MyApp<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This will add this component to the declarations parts of the module file.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>To use a module inside another module<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Edit AppModule and add import for MyModule<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">@NgModule {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Imports :[ CommonModule, MyModule]<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\"><li>To Use a component declared in MyModule in AppModule<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Edit MyModule and add exports for MyAppComponent<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">@NgModule {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Exports: [MyApp ]<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Import statement on top of the file is for the typescript compiler. Needed to link types.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Imports in @NgModule decorators is to tell angular which other modules can be used.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Service<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">ng generate service &lt;MyService&gt;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">creates two files<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">service.ts<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">service.spec.ts<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>@Injectable<\/strong>&nbsp;decorator tells it is a service<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To use a service in a module<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">@NgModule {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Providers<\/strong>: [ MyService ]<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To inject this service into a component, you need to change the constructor of the component<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">MyComponent {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">..<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">constructor (private serv : MyService ) { }<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This will automatically&nbsp;<strong>inject<\/strong>&nbsp;MyService in MyComponent.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is same as<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">private serv :MyService &nbsp;;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">constructor ( serv : MyService &nbsp;) {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">this. serv = serv;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Services are loaded in a common pool of injection context. So they are not bound to any particular module or component unlike components which are bound to a module which imports them.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So a service can be declared in any component and it can be used anywhere.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Scope of a Service<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Services are injected using a hierarchical injector in angular.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;Example. A has two children<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">a&#8212;-b, a&#8212;c<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1. If the provider&#8217;s array of each of the components will have the service name then all will be using a new instance of service [similar to prototype scope in spring ].<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2. If component A has service then all its children will get the same service instance. So if only the root component declares the provider for service, then it will be singleton. [the default in spring]<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Injecting Service into Another Service<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A service can be injected into another service if and if we use @Injectable() over it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">@injectable() is to be used on the service &#8211; which needs other services injected into it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Angular 6+ Services<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Instead of adding a service class to the providers[]&nbsp; array in AppModule , you can set the following config in @Injectable() :<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>@Injectable({providedIn: <\/strong><strong>&#8216;root&#8217;<\/strong><strong>})<\/strong><\/li><li>export class MyService { &#8230; }<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">This is exactly the same as:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>export class MyService { &#8230; }<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">and<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>import { MyService } from &#8216;.\/path\/to\/my.service&#8217;;<\/li><li>&nbsp;<\/li><li>@NgModule({<\/li><li>&nbsp;&nbsp;&nbsp;&#8230;<\/li><li>&nbsp;&nbsp;&nbsp;providers: [MyService]<\/li><li>})<\/li><li>export class MyService { &#8230; }<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Using Services for InterComponent Communicatio<\/strong>n<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Earlier we have seen using parent child, child parent communication. An alternative is service.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s say if a singleton service exists and we emit an event on its property. This can later be subscribed by some other component using the same service.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">this.myservice.subscribe ( something to do );<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Making a REST call<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Import HTTPClientModule in app-root.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">User DI to obtain HTTPClient<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">constructor (private http : HTTPClient ) { }<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">let response = http.get(\u201curl\u201d);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The response here is not a simple json object. It is an Observable which is an object that handles async calls.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">response.subscribe( () -&gt; console.log(\u201cobtained\u201d) )<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">or<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">response.subscribe( (json) -&gt; console.log(json) )<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So whenever we obtain the response from a web service we execute this function.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Building<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Ng build \u2013prod<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Routing<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Angular works on Single Page Application<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Ng new &lt;routing-component-name&gt; &#8211;routing<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You need to define path , component<br>a default can also be defined<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u201c*\u201d can be a path for component to open an error page.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&lt;router-outlet&gt;&lt;\/router-outlet&gt; loads the page at that required location.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For children \u2013 sub routing&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">{path : \u201csettings\u201d, component : ABCComponent&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Children {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">path : \u201cprofile\u201d, component : ProfileComponent &nbsp; \/\/ setttings\/profile<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">path : \u201csecurity\u201d, component : SecurityComponent \/\/ settings\/security<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&lt;a href=\u201d\/path\u201d&gt;&nbsp; &#8211; will refresh the entire page<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&lt;a routerLink = \u201c\/path\u201d &gt; &#8211; behaves as a SPA.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Type of Paths<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">.\/ or direct &#8211; relative path<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\/something &#8211; absolute path<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Styling Routes<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the tag that encloses &lt;a&gt; tag one can use&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&lt;li role=&#8221;presentation&#8221;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>routerLinkActive<\/strong>=&#8221;active&#8221;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[<strong>routerLinkActiveOptions<\/strong>]=&#8221;{exact: true}&#8221;&gt;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;a routerLink=&#8221;\/&#8221;&gt;Home&lt;\/a&gt;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;\/li&gt;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First option <strong>routerLinkActive <\/strong>sets the active style class and the other is used to set it to be true for exact match for absolute path. Otherwise all links that start with \u201c\/\u201d will be made active.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Navigating Programmatically<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Absolute Path<br>Inject Router in constructor<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">onLoadServer() {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;this.router.navigate([&#8216;\/servers&#8217;]);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;}<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\"><li>Relative Path &#8211; it needs the active routes information<br>Inject Router and ActivatedRoute in constructor<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">this.router.navigate([&#8216;servers&#8217;] , {relativeTo: this.activatedRoute} );<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>&nbsp;<\/strong><strong>Passing Params Dynamically<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Passing params<br>{ path: &#8216;servers\/:id&#8217;, component: ServerComponent)&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Retrieving Params<br>Inject ActivatedRoute<br>this.activatedroute.snapshot.params[&#8216;id&#8217;]<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Retrieving Params Reactively<br>If one of the same components tries to click on the link that tries to open the same component again &#8211; angular wouldn&#8217;t reload the component.<br>However, in order to reload\/re-instantiate the component one can subscribe to the changes in the params in ngOnInit().<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">this.paramsSubscription = this.route.params<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.subscribe(&nbsp; (<em>params<\/em>: Params) <em>=&gt;<\/em> {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.user.id = params[&#8216;id&#8217;]; &nbsp; &nbsp; &nbsp; }<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, also unsubscribe the event on ngOnDestroy()<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Passing Query Params and Fragments<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>using &lt;a&gt; tag<br>&lt;a&nbsp; [routerLink]=&#8221;[&#8216;\/servers&#8217;, server.id]&#8221;<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[<strong>queryParams<\/strong>]=&#8221;{allowEdit: server.id === 3 ? &#8216;1&#8217; : &#8216;0&#8217;}&#8221;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>fragment<\/strong>=&#8221;loading&#8221;&gt;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ server.name }}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;\/a&gt;&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">b.&nbsp; using function<br>this.router.navigate([&#8216;\/servers&#8217;, id, &#8216;edit&#8217;], {queryParams: {allowEdit: &#8216;1&#8217;}, fragment: &#8216;loading&#8217;});<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\/server\/1\/edit?allowEdit=1#loading<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Retrieving Query Params and Fragment<\/strong><br>Inject ActivatedRoute<br>this.activatedroute.snapshot.queryparams[&#8216;allowEdit&#8217;]<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Retrieving Params Reactively<br>Similar to params , one can subscribe to the changes in the query params in ngOnIt().<br><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Note : the params retrieved from url are in string. So if you consider passing them make sure to typecast them.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Preserving or merging query params on click of new link<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">queryParamsHandling: &#8220;preserve&#8221; &#8211; lets them remain same<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">queryParamsHandling: &#8220;merge&#8221;- merges any new params in the link<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Child Routes<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In case there are multiple components inside a component which are its children. So one can give routing in this manner.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">{ path: \u201c\u201d , component : HeadComponent, <strong>children <\/strong>: {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">{ path : \u201c\u201d , component : ChildComponent }<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">} } Note : the path here needs to be relative one as it is a child\u2019s path<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Add &lt;router-outlet&gt; to HeadComponent instead of ChildComponent\u2019s selector tag. This will load only child of this component.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Redirecting to non found page<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">{ path &#8220;not-found&#8221; , redirectTo : PageNotFoundComponent },<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">{ path &#8220;**&#8221; , redirectTo :&#8221; \/not-found&#8221; }<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">** is whatever path angular doesnt understand.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">redirectTo &#8211; changes the url to point to this<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">pathMatch: &#8216;full&#8217;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;Angular uses prefix matching by default. this can cause issues because &#8221; will match every URL.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>&nbsp;Router Guards<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;1. <strong>CanActivate Guard<\/strong> &#8211; To block access to certain URL&#8217;s which do not have the permission to access the resource.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Steps<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">a. Implement CanActivate in a class(this is called a Guard class) and implement the method canActivate() which returns a true or false.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">b. Invoke a service inside this method to decide on the access.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">c. Add this class to providers [because it acts a service]<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">d. In const of routes, add canActivate property and name the Guard class in array. There can be many Guards<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">path: &#8216;artist\/:artistId&#8217;,&nbsp; component: ArtistComponent, canActivate: [AlwaysAuthGuard]<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;2. <strong>CanActivateChild Guard<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For child routes &#8211; same steps only a different interface CanActivateChild needs to be implemented and different property<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">path: &#8216;artist\/:artistId&#8217;,&nbsp; component: ArtistComponent, canActivateChild: [AlwaysAuthChildGuard]<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">3.<strong> CanDeactivate Guard<\/strong> &#8211; suppose a user is changing form data and before saving, user tries to navigate away. In this scenario we can use CanDeactivate guard which will deactivate the route and open a Dialog Box to take user confirmation. This Guard is usally only a single one for application.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It works on Command Design pattern<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">a. Create a new interface which has only a&nbsp; canDeactivate() method that returns either an observable, promise or boolean.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">b. Make a new Guard class which extends CanDeactivate&lt;T&gt;. Above new interface will act as a generic type to CanDeactivate&lt;your new Interface type here&gt;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">c. In the class we will call the component&#8217;s canDeactivate method.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">d. Add this class to providers [because it acts a service]<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">e. Any class which needs to be Guarded needs to implement CanDeactivate&lt;Component&gt;.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">f. In const of routes, add canActivate property and name the Guard class in array. There can be many Guards<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">path: &#8216;artist\/:artistId&#8217;,&nbsp; component: ArtistComponent, canDeactivate: [DeActivateGuard]<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Passing data to a route can be done statically via (data : &#8220;string&#8221; in routes) and dynamically by a resolver ( intermediate code to be executed when a link has been clicked and before a component is loaded)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Observables<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Observables are data sources &#8211; could be events, Http requests, Triggers in code (All of which happen at a random time so its async in nature).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">These need to be handled by angular. An Observer handles them. An observer has three parts &#8211; normal execution, error situation , completion of execution.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So when you subscribe to a variable &#8211; &gt; the variable is itself an Observable and everything inside subscribe(&nbsp; observer: Observer&nbsp; ) is an observer.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">myobserver : Subscription = abc.subscribe()<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">onDestroy() {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.myobserver.unsubscribe();<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Observables need to be unsubscribed if they are created by the user.. Angular ones are automatically taken care of.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We pass values to an observer using observer.next( \/\/some value here );<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Subjects<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">They act as both observer and observable. They can be effective for communication between components. They can replace EventEmitter<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A simple class lets say MyService has an object on subject.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Two components A and B inject this service.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A can pass values to the subject.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">B can subscribe and read those values.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Pipe<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Transforms the output without changing the property<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">{{name | uppercase }}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can chain pipes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;You can make a custom pipe too by implementing PipeFilter interface or using ng generate pipe &lt;name&gt; command. Each pipe is initially impure (won&#8217;t react on changes if the value has changed). It can be made pure with a performance hit.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;You can have async pipes<\/p>\n\n\n","protected":false},"excerpt":{"rendered":"<p>Html \u2013 JS Dev vs Angular Dev Earlier we would keep these separate. We will manage the DOM via JS. So on click of a button, we will invoke a particular JS \u2013which might validate form inputs. Angular works in component based approach, we think in terms of components. So page is divided into components [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[97],"tags":[],"class_list":["post-1789","post","type-post","status-publish","format-standard","hentry","category-tech-learnings"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Angular 2+ &#187; Gaurav Wadhwani<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gauravw.com\/blog\/2021\/04\/angular-2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Angular 2+ &#187; Gaurav Wadhwani\" \/>\n<meta property=\"og:description\" content=\"Html \u2013 JS Dev vs Angular Dev Earlier we would keep these separate. We will manage the DOM via JS. So on click of a button, we will invoke a particular JS \u2013which might validate form inputs. Angular works in component based approach, we think in terms of components. So page is divided into components [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gauravw.com\/blog\/2021\/04\/angular-2\/\" \/>\n<meta property=\"og:site_name\" content=\"Gaurav Wadhwani\" \/>\n<meta property=\"article:published_time\" content=\"2021-04-10T10:13:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-04-10T10:13:06+00:00\" \/>\n<meta name=\"author\" content=\"Gaurav Wadhwani\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Gaurav Wadhwani\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"13 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/2021\\\/04\\\/angular-2\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/2021\\\/04\\\/angular-2\\\/\"},\"author\":{\"name\":\"Gaurav Wadhwani\",\"@id\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/#\\\/schema\\\/person\\\/9a05a9c3487f35f6b4577c6956cf252e\"},\"headline\":\"Angular 2+\",\"datePublished\":\"2021-04-10T10:13:00+00:00\",\"dateModified\":\"2021-04-10T10:13:06+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/2021\\\/04\\\/angular-2\\\/\"},\"wordCount\":3437,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/#\\\/schema\\\/person\\\/9a05a9c3487f35f6b4577c6956cf252e\"},\"articleSection\":[\"Tech Learnings\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/gauravw.com\\\/blog\\\/2021\\\/04\\\/angular-2\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/2021\\\/04\\\/angular-2\\\/\",\"url\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/2021\\\/04\\\/angular-2\\\/\",\"name\":\"Angular 2+ &#187; Gaurav Wadhwani\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/#website\"},\"datePublished\":\"2021-04-10T10:13:00+00:00\",\"dateModified\":\"2021-04-10T10:13:06+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/2021\\\/04\\\/angular-2\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gauravw.com\\\/blog\\\/2021\\\/04\\\/angular-2\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/2021\\\/04\\\/angular-2\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Angular 2+\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/\",\"name\":\"Gaurav Wadhwani\",\"description\":\"Where I write \\\/ scribble\",\"publisher\":{\"@id\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/#\\\/schema\\\/person\\\/9a05a9c3487f35f6b4577c6956cf252e\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/#\\\/schema\\\/person\\\/9a05a9c3487f35f6b4577c6956cf252e\",\"name\":\"Gaurav Wadhwani\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/788ed9666a6c4e011516ae9c744df4be274dcf933161c99a4ec7e06311d2d416?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/788ed9666a6c4e011516ae9c744df4be274dcf933161c99a4ec7e06311d2d416?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/788ed9666a6c4e011516ae9c744df4be274dcf933161c99a4ec7e06311d2d416?s=96&d=mm&r=g\",\"caption\":\"Gaurav Wadhwani\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/788ed9666a6c4e011516ae9c744df4be274dcf933161c99a4ec7e06311d2d416?s=96&d=mm&r=g\"},\"sameAs\":[\"http:\\\/\\\/gauravw.com\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Angular 2+ &#187; Gaurav Wadhwani","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/gauravw.com\/blog\/2021\/04\/angular-2\/","og_locale":"en_US","og_type":"article","og_title":"Angular 2+ &#187; Gaurav Wadhwani","og_description":"Html \u2013 JS Dev vs Angular Dev Earlier we would keep these separate. We will manage the DOM via JS. So on click of a button, we will invoke a particular JS \u2013which might validate form inputs. Angular works in component based approach, we think in terms of components. So page is divided into components [&hellip;]","og_url":"https:\/\/gauravw.com\/blog\/2021\/04\/angular-2\/","og_site_name":"Gaurav Wadhwani","article_published_time":"2021-04-10T10:13:00+00:00","article_modified_time":"2021-04-10T10:13:06+00:00","author":"Gaurav Wadhwani","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Gaurav Wadhwani","Est. reading time":"13 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/gauravw.com\/blog\/2021\/04\/angular-2\/#article","isPartOf":{"@id":"https:\/\/gauravw.com\/blog\/2021\/04\/angular-2\/"},"author":{"name":"Gaurav Wadhwani","@id":"https:\/\/gauravw.com\/blog\/#\/schema\/person\/9a05a9c3487f35f6b4577c6956cf252e"},"headline":"Angular 2+","datePublished":"2021-04-10T10:13:00+00:00","dateModified":"2021-04-10T10:13:06+00:00","mainEntityOfPage":{"@id":"https:\/\/gauravw.com\/blog\/2021\/04\/angular-2\/"},"wordCount":3437,"commentCount":0,"publisher":{"@id":"https:\/\/gauravw.com\/blog\/#\/schema\/person\/9a05a9c3487f35f6b4577c6956cf252e"},"articleSection":["Tech Learnings"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/gauravw.com\/blog\/2021\/04\/angular-2\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/gauravw.com\/blog\/2021\/04\/angular-2\/","url":"https:\/\/gauravw.com\/blog\/2021\/04\/angular-2\/","name":"Angular 2+ &#187; Gaurav Wadhwani","isPartOf":{"@id":"https:\/\/gauravw.com\/blog\/#website"},"datePublished":"2021-04-10T10:13:00+00:00","dateModified":"2021-04-10T10:13:06+00:00","breadcrumb":{"@id":"https:\/\/gauravw.com\/blog\/2021\/04\/angular-2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gauravw.com\/blog\/2021\/04\/angular-2\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gauravw.com\/blog\/2021\/04\/angular-2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gauravw.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Angular 2+"}]},{"@type":"WebSite","@id":"https:\/\/gauravw.com\/blog\/#website","url":"https:\/\/gauravw.com\/blog\/","name":"Gaurav Wadhwani","description":"Where I write \/ scribble","publisher":{"@id":"https:\/\/gauravw.com\/blog\/#\/schema\/person\/9a05a9c3487f35f6b4577c6956cf252e"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gauravw.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/gauravw.com\/blog\/#\/schema\/person\/9a05a9c3487f35f6b4577c6956cf252e","name":"Gaurav Wadhwani","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/788ed9666a6c4e011516ae9c744df4be274dcf933161c99a4ec7e06311d2d416?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/788ed9666a6c4e011516ae9c744df4be274dcf933161c99a4ec7e06311d2d416?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/788ed9666a6c4e011516ae9c744df4be274dcf933161c99a4ec7e06311d2d416?s=96&d=mm&r=g","caption":"Gaurav Wadhwani"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/788ed9666a6c4e011516ae9c744df4be274dcf933161c99a4ec7e06311d2d416?s=96&d=mm&r=g"},"sameAs":["http:\/\/gauravw.com"]}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/gauravw.com\/blog\/wp-json\/wp\/v2\/posts\/1789","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gauravw.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gauravw.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gauravw.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gauravw.com\/blog\/wp-json\/wp\/v2\/comments?post=1789"}],"version-history":[{"count":1,"href":"https:\/\/gauravw.com\/blog\/wp-json\/wp\/v2\/posts\/1789\/revisions"}],"predecessor-version":[{"id":1790,"href":"https:\/\/gauravw.com\/blog\/wp-json\/wp\/v2\/posts\/1789\/revisions\/1790"}],"wp:attachment":[{"href":"https:\/\/gauravw.com\/blog\/wp-json\/wp\/v2\/media?parent=1789"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gauravw.com\/blog\/wp-json\/wp\/v2\/categories?post=1789"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gauravw.com\/blog\/wp-json\/wp\/v2\/tags?post=1789"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}