I am working on an Angular 6 project.
I need to communicate between parent and child components(from parent to child) but actually by using @Output I could not achieve this.
Please help me regarding the below codes.
child component:survey.component.html
<div style="text-align:center">
<app-root (numberGenerated)='selectValue($event)'></app-root>
survey.component.ts
import { Component, OnInit, SkipSelf , Input, Output , EventEmitter} from '@angular/core';
import { AppComponent } from '../parent/app.component'
@Component({
selector: 'app-survey',
templateUrl: './survey.component.html',
styleUrls: ['./survey.component.css']
})
export class SurveyComponent implements OnInit {
selectValue( newValue : any ) {
console.log(newValue);
}
constructor(){}
ngOnInit() {
}
}
parent component: app.component.ts
import { Component, Input , Output , EventEmitter } from '@angular/core';
import { Router } from '@angular/router';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'BegumSurvey';
@Output() private numberGenerated = new EventEmitter<number>();
public generateNumber() {
const randomNumber = Math.random();
this.numberGenerated.emit(randomNumber);
}
constructor(){
}
ngOnInit() {
}
}
app.component.html
<button class="btn" (click)="generateNumber()">Fire event!</button>
Could you please help me to understand why even 'Fire event!' is not printed?
Thanks much.
Any help is appreciated.
Begum