You need to work with communication between components. First you issue an event by app-header
, after that, we will receive the value issued in the parent component (Appcomponent) and then send it to the component app-banner
.
See working on Stackblitz: https://angular-emk7hj.stackblitz.io
app.component.html
<app-banner [enable]="enable"></app-banner>
<app-header (buttonClick)="handleButtonClick($event)"></app-header>
app.componentts.
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
enable:boolean = false;
handleButtonClick(value) {
this.enable = value;
}
}
banner.component.html
<p *ngIf="enable">
banner works!
</p>
banner.componentts.
@Component({
selector: 'app-banner',
templateUrl: './banner.component.html',
styleUrls: ['./banner.component.css']
})
export class BannerComponent implements OnInit {
@Input() enable:boolean;
}
header.component.html
<div class="header">
<button (click)="displayBanner()">Entrar</button>
</div>
header.componentts.
@Component({
selector: 'app-header',
templateUrl: './header.component.html',
styleUrls: ['./header.component.css']
})
export class HeaderComponent implements OnInit {
@Output() buttonClick = new EventEmitter()
enable:boolean = false;
displayBanner(){
this.enable = this.enable == false ? true : false;
this.buttonClick.emit(this.enable);
}
}
Thanks for the renanzin return!
– Munir Baarini
but is showing errors on the console
– Munir Baarini
ERROR in src/app/banner/banner.component.ts(8,14): error TS2420: Class 'Bannercomponent' incorrectly Implements interface 'Oninit'. Property 'ngOnInit' is Missing in type 'Bannercomponent'. src/app/banner/banner.component.ts(10,4): error TS2304: Cannot find name 'Input'. src/app/header/header.component.ts(10,14): error TS2420: Class 'Headercomponent' incorrectly Implements interface 'Oninit'.
– Munir Baarini
Property 'ngOnInit' is Missing in type 'Headercomponent'. src/app/header/header.component.ts(12,4): error TS2304: Cannot find name 'Output'. src/app/header/header.component.ts(12,31): error TS2304: Cannot find name 'Eventemitter'.
– Munir Baarini
Can you tell me where I went wrong?
– Munir Baarini
You are importing the Oninit interface?
– renanvm
import { OnInit } from '@angular/core';
– renanvm
this way: import { Component, Oninit } from '@angular/core';
– Munir Baarini
imported in the above form into each component
– Munir Baarini
at least in the two who are giving errors to import this so
– Munir Baarini
Output and Eventemitter seems not being imported
– renanvm
these did not matter, as I please?
– Munir Baarini
Let’s go continue this discussion in chat.
– renanvm