angular 8 pass data from parent to child component code example
Example 1: angular send data to parent component
import { Component, Output, EventEmitter } from '@angular/core';
@Component({
selector: 'app-child',
template: `
<button (click)="sendMessage()">Send Message</button>
`,
styleUrls: ['./child.component.css']
})
export class ChildComponent {
message: string = "Hola Mundo!"
@Output() messageEvent = new EventEmitter<string>();
constructor() { }
sendMessage() {
this.messageEvent.emit(this.message)
}
}
Example 2: input property angular
<app-slider [title]="'This is a title'"> </app-slider>
.
.
@input() title: string;
constructor(){
}
<div id='slider' class='slider-big'>
<h1> {{title}}</h1>
</div>
Example 3: how to pass variable via component after it's updated angular
<app-child
[data]="data"
(filterEmmiter)="filter($event)">
</app-child>
import {Component, OnChanges, SimpleChanges, Input} from '@angular/core';
class Child implements OnInit, OnChanges {
@Input() Data: any;
constructor() {}
ngOnchanges(changes: SimpleChanges) {
console.log(changes);
}
}