We will have a short look of angular.io

Why AngularJS-2/4?

So What is AngularJS?

Before starting AngularJS 2/4 (Pre - Requisite)

System configuration

Install Node.js® and NPM if they are not already on your machine.

  1. NODE – it is JavaScript runtime built on Chrome’s V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model (it makes javascript as serverside language & bring javascript language as out of from browser to communicate with os)
    node -version –> 6.9.x & above (keep it as latest)
  2. NPM – node package manager
    npm -version –> 3.x.x and above (keep it as latest)
  3. Some of the global packages installation if it is missing form npm or might me needs to it in updated version.

Modules –> @NgModule (@ - refers as a typescript decorator)

Angular apps are modular and Angular has its own modularity system called NgModules.
Every Angular app has at least one NgModule class, the root module, conventionally named AppModule.
And this AppModule can be divided into feature modules to maintain performance (lazy loading).

	import { NgModule }      from '@angular/core';
	import { BrowserModule } from '@angular/platform-browser';
	@NgModule({
	  imports:      [ BrowserModule ],
	  providers:    [ Logger ],
	  declarations: [ AppComponent ],
	  exports:      [ AppComponent ],
	  bootstrap:    [ AppComponent ]
	})
	export class AppModule { }

Component :

A component controls a patch of screen called a view.

Data binding

Model to View or View to Model data bindings

Component Interaction

Interaction/Communication between parent and child components.

  1. Pass data from parent to child with input property binding – @Input('master') masterName: string;<my-div [master]="master">.
  2. Intercept input property changes with ngOnChanges() method – ngOnChanges will execute if any input property got changed in parent components – ngOnChanges(changes: SimpleChange).
  3. Parent listens for child event – (onVoted)="onVoted($event)"@Output() onVoted = new EventEmitter<boolean>();this.onVoted.emit(true); // this will call parent component 'onVoted' method
  4. Parent interacts with child via local variable – But limited to access only in template itself.
    You can do both by creating a template reference variable for the child element and then reference that variable within the parent template.
    Template reference variable#fax is same as ref-fax. the scope of this variable wiil be for entire template
    <button (click)="timer.stop()">Stop</button> // calling child components method
    <app-countdown-timer #timer></app-countdown-timer> //child components

  5. Parent calls an @ViewChild() in component (to solve above problem) –
    @ViewChild(CountdownTimerComponent) private timerComponent: CountdownTimerComponent; – child component needs to inject in parent component.
    start() { this.timerComponent.start();} //calling child components method

  6. Parent and children communicate via a angulaar service –
    • A parent component and its children share a service whose interface enables bi-directional communication within the family. (Recommended one)
    • Create service file & inject in both components
    • Either observable by RxJS or putting simple getters & setters of into service class

Directives

Directives allow you to attach behavior to elements in the DOM

import { Directive } from '@angular/core';
@Directive({
	selector: '[appHighlight]'   // It's the brackets ([]) that make it an attribute selector
})
export class HighlightDirective {
	constructor() { }
}

@Directive({ 
	selector?: string
	inputs?: string[]
	outputs?: string[]
	host?: {[key: string]: string}
	providers?: Provider[]
	exportAs?: string
	queries?: {[key: string]: any}
})

-- instead of host property above-- 
@HostListener('mouseenter') onMouseEnter() {
	this.highlight(this.highlightColor || 'red');
}

Services

LoggerService -:

export class LoggerService {
	log(msg: any)   { console.log(msg); }
	error(msg: any) { console.error(msg); }
	warn(msg: any)  { console.warn(msg); }
}

Dependency injection

Change Detection & Angular life cycle hooks

Lifecycle hooks

A component has a lifecycle managed by Angular.

#Follow Me


Github | Twitter | LinkedIn | More Blogs

If you have any comments #Comments Here...