Angular

Angular Samples

Home

1. Create New Component
2. Component To HTML
3. @Input
4. Button Click Event
5. @Output
6. Template Variable
7. *ngFor
8. Safe Navigation Operator
9. *ngIf
10. Hidden Property
11. *ngSwitch
12. Service
13. Routing
14. Routing with Parameter
15. Router Link
16. Module
17. Lazy Loaded Module

Communicating with parent component using @Output

my-second-component.component.html


<h2>Employee Details</h2>
<div>
<div>
	 {{ employee.id }} 
<hr/>
	 {{ employee.name }} 
</div>
<div>
<button class="btn btn-primary" (click)="handleClickMe()" >Click Me!</button>
</div>
</div>

my-second-component.component.ts


import { Component, OnInit, Input,Output,EventEmitter } from '@angular/core';

@Component({
	selector: 'app-my-second-component',
	templateUrl: './my-second-component.component.html',
	styleUrls: ['./my-second-component.component.css']
})

export class MySecondComponentComponent implements OnInit {

	@Input() employee:any

	@Output() eventClick = new EventEmitter()

	constructor() { }

	ngOnInit() {
	}

	handleClickMe(){
		this.eventClick.emit("Akshay")
	}
}

my-first-component.component.html


<app-my-second-component [employee]="employee1" (eventClick)="handleClickMe($event)"></app-my-second-component>

my-first-component.component.ts


import { Component, OnInit } from '@angular/core';

@Component({
	selector: 'app-my-first-component',
	templateUrl: './my-first-component.component.html',
	styleUrls: ['./my-first-component.component.css']
})

export class MyFirstComponentComponent implements OnInit {

	constructor() { }

	ngOnInit() {
	}
	
	employee1={
	id:1,
	name:'Akshay Patel'
	}
	
	handleClickMe(data){
		console.log("Received: " + data)
	}
}

app.component.html


<app-my-first-component></app-my-first-component>