1

I have button on one component that triggers function:

<button class="btn-main" (click)="hideElement()"></button>

In another component that is popup (and no parent-child connection), I have 3 divs like this

 <div class="element1"></div>

and I want one to be hidden (set class with visibility: hidden) when I click on button in parent. How can I hide that div when I open popup with that button?

2 Answers 2

0

in child template:

<div class="element1" #element1 ></div>

in child component:

@ViewChild('element1') element1: ElementRef;

in parent:

@ViewChild(ChildComponent) child: ChildComponent;

constructor(private renderer: Renderer2) {}

hideElement() {
  this.renderer.setStyle(child.element1 , 'visibility' , 'hidden');
}
Sign up to request clarification or add additional context in comments.

Comments

0

Method 1 - Parent-child relationship

You can hide or show the div in the child component with a hideDiv property, and make that property available for data binding with the @Input decorator. The property can toggle the visibility style directly or apply a class to the div:

@Component({
  selector: 'child-component',
  template: `
    <div></div>
    <div [class.hiddenClass]="hideDiv"></div>
    <div [style.visibility]="hideDiv ? 'hidden' : 'visible'"></div>
    <div></div>
  `,
  styles: [`.hiddenClass { visibility: hidden; }`]
})
export class ChildComponent {
  @Input() hideDiv = false;
}

You would then modify it with data binding when clicking on the button in the parent component:

@Component({
  selector: 'parent-component',
  template: `
    <button (click)="hideChildDiv = !hideChildDiv">Toggle div visibility</button>
    <child-component [hideDiv]="hideChildDiv" ></child-component>
  `
})
export class ParentComponent {
  hideChildDiv = false;
}

You can test the code in this stackblitz.


Method 2 - Communication between two components with a service

You can use a service to allow two separate components to communicate with each other or to share some information. See this stackblitz for a demo.

Service:

import { Injectable } from "@angular/core"

@Injectable()
export class VisibilityService {
  hideDiv = false;
}

Parent component:

@Component({
  selector: 'app-root',
  template: `
    <child1></child1>
    <child2></child2>
  `
})
export class AppComponent {
}

Child1 component:

import { VisibilityService } from "./visibility.service";

@Component({
  selector: 'child1',
  template: `
    <button (click)="hideDiv = !hideDiv">Toggle div visibility</button>
  `
})
export class Child1Component {

  public get hideDiv(): boolean {
    return this.visibilityService.hideDiv;
  }

  public set hideDiv(value: boolean) {
    this.visibilityService.hideDiv = value;
  }

  constructor(private visibilityService: VisibilityService) { }
}

Child2 component:

import { VisibilityService } from "./visibility.service";

@Component({
  selector: 'child2',
  template: `
    <div class="div1"></div>
    <div class="div1" [class.hiddenClass]="hideDiv"></div>
    <div class="div1"></div>
    <div class="div1" [style.visibility]="hideDiv ? 'hidden' : 'visible'"></div>
    <div class="div1"></div>
  `
})
export class Child2Component {

  public get hideDiv(): boolean {
    return this.visibilityService.hideDiv;
  }

  constructor(private visibilityService: VisibilityService) { }
}

Module:

...

import { AppComponent } from './app.component';
import { Child1Component } from './child1.component';
import { Child2Component } from './child2.component';
import { VisibilityService } from "./visibility.service";

@NgModule({
  declarations: [ 
    AppComponent,
    Child1Component,
    Child2Component
  ],
  providers: [
    VisibilityService
  ],
  ...
})
export class AppModule { }

7 Comments

Im getting message that property visibilityService does not exist on type Child1 ...I have service in appmodule, imported in component, but gets that error ... Is there option to work only with one component with button, service, and other component with affected div? Here, I have parent, service and 2 children? :) Thanks in advance
Make sure that you declare the service with private visibilityService: VisibilityService in the constructor of each component class. Don't forget private.
Yes, i have that, with private, i use that service already for some other data
Then I don't know why you get the error. You can compare your code with the stackblitz (which works with no error) to see where they differ.
Difference with your stackblitz and my code is that i dont have parent component that holds 2 children, i just have 2 components, first with button, and second with div that should be hidden on button click ...Does that change things somehow?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.