Hiding another component element in Angular

Asked

Viewed 62 times

0

Good evening person, I’m on a project where I wanted to add that button that hides and shows some information. But the tricky thing is that this button is in one component and the information to be hidden is in another. For example:

header.componente.html

<div>
        <div class="btn-hidden btn-feature">
          <img title="Ocultar" (click)="btnHide()">
        </div>
</div>

header.componentts.

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

@Component({
  selector: 'app-header-dashboard',
  templateUrl: './header-dashboard.component.html',
  styleUrls: ['./header-dashboard.component.sass']
})
export class HeaderDashboardComponent implements OnInit {

   @Input() showValue;

  constructor() { }

  btnHide() {
    console.log(this.showValue);
    this.showValue = !this.showValue;
  }
}

Here would be the component with the information to be hidden

Details.component.html

<div>
    <p *ngIf="!showValue" class="card-account-amount">{{ value | currency: 'BRL' }}</p>
    <p *ngIf="showValue" class="card-account-amount">R$ --</p>
</div>

Details.component.ts

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

@Component({
  selector: 'app-account',
  templateUrl: './account.component.html',
  styleUrls: ['./account.component.sass']
})
export class AccountComponent implements OnInit {

  @Input() showValue = true;

  constructor() { }

  ngOnInit(): void {
  }

}

So it worked in a way, if I switch to showValue = false or showValue = true, change the values here:

<div>
    <p *ngIf="!showValue" class="card-account-amount">{{ value | currency: 'BRL' }}</p>
    <p *ngIf="showValue" class="card-account-amount">R$ --</p>
</div>

But it does not change the values by clicking on the button of the Header component, which I am locked in. Only if I change the.ts file of Accountcomponent

  • That’s right, to pass data from parent component to son you use Input() and from son to father you use Output() with eventEmitter().

  • can give me an example with the code above?

  • I edited the question to be clearer

1 answer

0

I made several changes and this time I tested before, and here it seems to me to have worked now.

header-dashboad-Component.ts

import { Component, OnInit, ViewChild } from '@angular/core';
import { AccountComponent } from "../../users/account/account.component";

@Component({
    selector: 'app-header-dashboard',
    templateUrl: './header-dashboard.component.html',
    styleUrls: ['./header-dashboard.component.css'],
})
export class HeaderDashboardComponent implements OnInit {

    @ViewChild(AccountComponent) accountComponent: AccountComponent;

    constructor() { }

    ngOnInit(): void { }

    btnHide() {
        let current = this.accountComponent.showValue;
        this.accountComponent.showValue = (current) ? false : true;
    }

}

header-Dashboard-Component.html

<p>header-dashboard works!</p>

<button (click)="btnHide()">Hide Values</button>

<app-account>
    <!-- This is a child of to Up -->
</app-account>

Account-Component.ts

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

@Component({
    selector: 'app-account',
    templateUrl: './account.component.html',
    styleUrls: ['./account.component.css']
})
export class AccountComponent implements OnInit {

    public showValue = false;

    constructor() { }

    ngOnInit(): void { }

}

Account-Component.html

<p *ngIf="showValue">account works!</p>

<p [textContent]="showValue"></p>

Source: https://angular.io/api/core/ViewChild

  • didn’t work out :/

  • I edited the question to be clearer

  • I did it again and I tested it, you test it and you tell me later. ...

  • on the console gave the one ERROR Typeerror: Cannot read Property 'showValue' of Undefined

  • It happened to me too, you adjust by placing the header’s html control <app-Account></app-Account>. But I think you need to review your structure, because these two components in my humble opinion have no relation to each other. But do it at least to test...

  • Inside the header html I am using a <router-outlet> because there is a div inside the header that alternates in several pages below the header itself, one of them being the app-Account, understand?

Show 1 more comment

Browser other questions tagged

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