Display a component after a click($event) Angular 4

Emile Cantero picture Emile Cantero · May 30, 2017 · Viewed 70.2k times · Source

I'm trying to create an event on a click button who fired another component, if you click again it reduce the component (a part of the displayed component is always visible).. I know this could be done with [ngClass]='hidden' and everything in the same component but I am not sure whether its the best way in terms of modularity. Thanks in advance

Here's my html:

<div class="daydetail">
     <h1>Detail of the day</h1>
     <button type="button" label="Click"(click)="display('my-displaychild')"></button>
     <div>{{my-displaychild}}</div>
</div> 

Here's is my component:

import { DisplayChildComponent } from './displaychild.component';

export class AppliV2Component   { 


    display(vid:DisplayChildComponent) {
           console.log(DisplayChildComponent);
    }


 }

Answer

Hamed Baatour picture Hamed Baatour · May 30, 2017

I think you should keep it simple with the use of *ngIf and you can pass in the boolean value to the child component to hide only the part you want using the @Input decorator

1.parent HTML

<div class="daydetail">
     <h1>Detail of the day</h1>
     <button type="button" label="Click" (click)="toggleChild()"></button>
     <div>
         <child-component [showMePartially]="showVar"></child-component>
     </div>
</div>

2.parent component

export class AppliV2Component {
    showVar: boolean = true;

    toggleChild(){
        this.showVar = !this.showVar;
    }
 }

3.child component

export class ChildComponent {
    @Input() showMePartially: boolean;

    // don't forget to import Input from '@angular/core'
}

4.child HTML

<div>
    <h1> this part is always visible</h1>
</div>

<div *ngIf="showMePartially">
    <h1> this part will be toggled by the parent component button</h1>
</div>