Hiding Component in Angular 7

Asked

Viewed 704 times

1

Good night, you guys! I started studying Angular recently and came across a problem that I can’t solve, I need that when clicking a button inside the header component, the banner component is hidden.

This is the structure of the app.component.html:

<app-header></app-header>
<app-banner></app-banner>

This is the Header.html component:

<div class="header">
   <button>Entrar</button>
</div>

This is the Banner.html component

<div>
  <p>banner works!</p>
</div>

Can someone please help me?

1 answer

2


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!

  • but is showing errors on the console

  • 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'.

  • 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'.

  • Can you tell me where I went wrong?

  • You are importing the Oninit interface?

  • import { OnInit } from '@angular/core';

  • this way: import { Component, Oninit } from '@angular/core';

  • imported in the above form into each component

  • at least in the two who are giving errors to import this so

  • Output and Eventemitter seems not being imported

  • these did not matter, as I please?

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.