Let's learn Angular 2 by an Examples and not spending much time in theory's. We will cover the best practical and ready to use Angular 2 Examples.
You can find some other posts of this series below:
Component in Angular2 (Part 1 of 5)
Directive and Pipes in Angular2 (Part 2 of 5)
Forms and Pipes in Angular2 (Part 3 of 5)
Dependency Injection in Angular2 (Part 4 of 5)
HTTP and Routing in Angular2 (Part 5 of 5)
All example are based on Angular2 RC 1
Components - Displaying data in our templates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component } from '@angular/core';
@Component({
moduleId: module.id,
selector: 'test5-app',
templateUrl: 'test5.component.html',
styleUrls: ['test5.component.css']
})
export class Test5AppComponent {
stringX: string;
arrayY: string[];
objectZ: any;
constructor() {
this.stringX = 'test5 works!';
this.arrayY = ['one', 'two', 'three'];
this.objectZ = [{
name: 'name 1',
src: 'src 2'
}, {
name: 'name 1',
src: 'src 2'
}]
}
}
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1>
{{stringX}}
</h1>
<ul>
<li *ngFor="let item of arrayY">
{{item}}
</li>
</ul>
<ul>
<li *ngFor="let item of objectZ">
{{item.name}} -> {{item.src}}
</li>
</ul>
Components - Working with Events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component } from '@angular/core';
@Component({
moduleId: module.id,
selector: 'test5-app',
templateUrl: 'test5.component.html',
styleUrls: ['test5.component.css']
})
export class Test5AppComponent {
stringX: string;
arrayY: string[];
constructor() {
this.stringX = 'test5 works!';
this.arrayY = ['one', 'two', 'three'];
};
onclick(myInput) {
this.arrayY.push(myInput);
};
updateH1(obj) {
this.stringX = obj;
};
}
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1>
{{stringX}}
</h1>
<input #myInput (keyup.enter)="onclick(myInput.value)" (blur)="onclick(myInput.value)">
<button (click)="onclick(myInput.value)">Add</button>
<ul>
<li (click)="updateH1(item)" *ngFor="let item of arrayY">
{{item}}
</li>
</ul>
Components - Using Property
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component } from '@angular/core';
@Component({
moduleId: module.id,
selector: 'test5-app',
templateUrl: 'test5.component.html',
styleUrls: ['test5.component.css']
})
export class Test5AppComponent {
stringX: string;
arrayY: string[];
constructor() {
this.stringX = 'test5 works!';
this.arrayY = ['one', 'two', 'three'];
};
onclick(myInput) {
this.arrayY.push(myInput);
};
updateH1(obj, artistContainer) {
this.stringX = obj;
artistContainer.style.backgroundColor = "#FEC";
};
}
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1 [innerHtml]="'Name: ' + stringX">
</h1>
<input #myInput (keyup.enter)="onclick(myInput.value)" (blur)="onclick(myInput.value)">
<button (click)="onclick(myInput.value)">Add</button>
<ul>
<li #artistContainer (click)="updateH1(item,artistContainer)" *ngFor="let item of arrayY">
{{item}}
</li>
</ul>
Components - Using more complex data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component } from '@angular/core';
interface Artist {
name: string;
shortname: string;
reknown: string;
bio: string;
}
@Component({
moduleId: module.id,
selector: 'test5-app',
templateUrl: 'test5.component.html',
styleUrls: ['test5.component.css']
})
export class Test5AppComponent {
artists = ARTISTS;
}
var ARTISTS: Artist[] = [{
"name": "Mohammed Nisar",
"shortname": "My shrt name 1",
"reknown": "Royal acadamy of painting",
"bio": "My bio 1"
}, {
"name": "Kumar King",
"shortname": "My shrt name 2",
"reknown": "Royal acadamy of Design",
"bio": "My bio 2"
}, {
"name": "Honey Sing",
"shortname": "My shrt name 3",
"reknown": "Royal acadamy of Dance",
"bio": "My bio 3"
}]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<ul>
<li *ngFor="let item of artists">
{{item.name}}
</li>
</ul>
Components - Using Sub-Component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, Input } from '@angular/core';
import { NewCmpComponent } from './new-cmp';
interface Artist {
name: string;
shortname: string;
reknown: string;
bio: string;
}
@Component({
moduleId: module.id,
selector: 'test5-app',
templateUrl: 'test5.component.html',
styleUrls: ['test5.component.css'],
directives: [NewCmpComponent]
})
export class Test5AppComponent {
artists = ARTISTS;
}
var ARTISTS: Artist[] = [{
"name": "Mohammed Nisar",
"shortname": "My shrt name 1",
"reknown": "Royal acadamy of painting",
"bio": "My bio 1"
}, {
"name": "Kumar King",
"shortname": "My shrt name 2",
"reknown": "Royal acadamy of Design",
"bio": "My bio 2"
}, {
"name": "Honey Sing",
"shortname": "My shrt name 3",
"reknown": "Royal acadamy of Dance",
"bio": "My bio 3"
}]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<ul>
<li *ngFor="let item of artists">
<app-new-cmp [artist]="item">Loading.</app-new-cmp>
</li>
</ul>
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {
Component,
OnInit,
Input
} from '@angular/core';
@Component({
moduleId: module.id,
selector: 'app-new-cmp',
templateUrl: '../new-cmp/new-cmp.component.html',
styleUrls: ['new-cmp.component.css'],
inputs: ['artist']
})
export class NewCmpComponent {
componentName: 'NewCmpComponent'
}
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div>
<h2>{{artist.name}}</h2>
</div>
Components - Getting data to the component with input
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<story-app>
loading...
</story-app>
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class Character{
constructor(public id: number, public name: string){ }
}
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component ,Input} from '@angular2/core';
import { ChildComponent } from './child.component';
import { Character } from './character';
@Component({
selector: 'story-app',
template: `
<div style = "border:2px solid orange; padding:5px;">
<h1>Parent Component</h1>
<input [(ngModel)] = "characters[0].name"/>
<button (click)="select(characters[0])">Input</button>
<br/><br/>
<story-characters [character]="selectedCharacter"></story-characters>
</div>
`,
directives:[ChildComponent]
})
export class ParentComponent{
characters = [
{
"id":11,
"name":"Name"
}];
selectedCharacter: Character;
select(selectedCharacter : character){
this.selectedCharacter = selectedCharacter;
}
}
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, Input} from '@angular2/core';
import { Character} from './character';
@Component({
selector: 'story-characters',
template:`
<div style="border:2px solid orange; padding:5px">
<h1>Child Component: <span *ngIf="character">{{character.name}}</span></h1>
</div>
`,
inputs:['character']
})
export class ChildComponent {
public character: Character;
}
Components - Subscribing to component events with output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<parent-app>
loading...
</parent-app>
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class Character{
constructor(public id:number,public name:string){}
}
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Component} from '@angular2/core';
import {ChildComponent} from './child.component';
@Component({
selector:'parent-app',
template:`
<div style="border:2px solid orange; padding:5px;">
<h1>Parent Component: {{parent}}</h1>
<child-app (changed)=changed($event)></child-app>
</div>
`,
directives:[ChildComponent]
})
export class AppComponent{
changed(changedCharacter:any){
if(changedCharacter){
this.parent = changedCharacter.name;
}
}
}
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Component, EventEmitter, Output} from '@angular2/core';
import{ Character } from './character';
@Component({
selector:'child-app',
template:`
<div style = "border:2px solid orange; padding:5px;">
<h1>Child Component</h1>
<input [(ngModel)]="characters[0].name">
<button (click)="select(characters[0])">Output</button>
</div>
`,
outputs : ['changed']
})
export class ChildComponent implements OnInit{
public changed = new EventEmitter<Character>();
characters = [
{
"id":11,
"name":"Nisar"
}
];
selectedCharacter : Character;
select(selectedCharacter: Character){
this.changed.emit(selectedCharacter);
}
}
Components - Getting data to the component with @input
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<story-app>
loading...
</story-app>
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class Character{
constructor(public id: number, public name: string){ }
}
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component ,Input} from '@angular2/core';
import { ChildComponent } from './child.component';
import { Character } from './character';
@Component({
selector: 'story-app',
template: `
<div style = "border:2px solid orange; padding:5px;">
<h1>Parent Component</h1>
<input [(ngModel)] = "characters[0].name"/>
<button (click)="select(characters[0])">Input</button>
<br/><br/>
<story-characters [character]="selectedCharacter"></story-characters>
</div>
`,
directives:[ChildComponent]
})
export class ParentComponent{
characters = [
{
"id":11,
"name":"Name"
}];
selectedCharacter: Character;
select(selectedCharacter : character){
this.selectedCharacter = selectedCharacter;
}
}
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, Input} from '@angular2/core';
import { Character} from './character';
@Component({
selector: 'story-characters',
template:`
<div style="border:2px solid orange; padding:5px">
<h1>Child Component: <span *ngIf="character">{{character.name}}</span></h1>
</div>
`
})
export class ChildComponent {
@Input() character: Character;
}
Components - Subscribing to component events with @output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<parent-app>
loading...
</parent-app>
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class Character{
constructor(public id:number,public name:string){}
}
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Component} from '@angular2/core';
import {ChildComponent} from './child.component';
@Component({
selector:'parent-app',
template:`
<div style="border:2px solid orange; padding:5px;">
<h1>Parent Component: {{parent}}</h1>
<child-app (changed)=changed($event)></child-app>
</div>
`,
directives:[ChildComponent]
})
export class AppComponent{
changed(changedCharacter:any){
if(changedCharacter){
this.parent = changedCharacter.name;
}
}
}
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Component, EventEmitter, Output} from '@angular2/core';
import{ Character } from './character';
@Component({
selector:'child-app',
template:`
<div style = "border:2px solid orange; padding:5px;">
<h1>Child Component</h1>
<input [(ngModel)]="characters[0].name">
<button (click)="select(characters[0])">Output</button>
</div>
`
})
export class ChildComponent implements OnInit{
@Output() changed = new EventEmitter<Character>();
characters = [
{
"id":11,
"name":"Nisar"
}
];
selectedCharacter : Character;
select(selectedCharacter: Character){
this.changed.emit(selectedCharacter);
}
}
I really like reading through a post that can make people think.
ReplyDeleteAngularjs Training in Bangalore , Angular 2 Training in bangalore , Python Training in Bangalore