How the parent component controls the display and hiding of child components in angular

effect description: when the screen size is smaller than 768px, the chat box is hidden first, click the chat button to show the chat box, click the close button to close the chat box
clipboard.png

parent component code:
parent component binding event:
< li class= "list-group-item" >

        <button class="btn btn-block btn-primary" *ngIf="show" (voted)="onVoted($event)"></button>
      </li>

declarations and methods in the parent component class file:
show = true;

onVoted (agreed: boolean) {

this.show = agreed;

}

subcomponent code:
subcomponent binding event:
< button class= "btn btn-primary" (click) = "vote ()" > close < / button >
methods in the subcomponent class file:
@ Output () voted = new EventEmitter < boolean > ();

vote () {

this.voted.emit(false);

}

May.14,2021

I am not enough to help you solve the problem with the information you give, but I can see several of your current problems

  • * ngIf this controls the display / shutdown of the button, not the control subcomponent.
  • Control Show / hide it is best to use [hidden] = "expression" to operate, which reduces DOM operations and improves efficiency.

I hope you can give me more information, such as parent-child components, so that it can help you.


the parent component controls the closing and hiding of the child component. You must bind a variable controlled by the parent component to the child component.

the < button > tag here is a subcomponent, which is obviously wrong. Your subcomponent is obviously the chat component below.

parent component

html:
<li class="list-group-item">
   <button class="btn btn-block btn-primary" (click)="onVoted($event)"></button>
</li>

<!--  -->
<div [style.display]="isVisible ? 'block':'none' ">
    <input />
    ....
</div>

ts: 
public isVisible:boolean;

public onVoted() {
  this.isVisible = !this.isVisible;
}

of course you can use * ngIf, just look at your business. [style.display] may be more suitable for you

it is recommended to take a look at the document ide/component-interaction" rel=" nofollow noreferrer "> component interaction


you can add a style binding [style.display] = "show" directly in the child component;
set the property to "none"
directly in the css in the parent component, and then directly set the property to "block" in the click event in the parent component


can't you just use ngIf directly? DEMO

is it because I misunderstood your description of the problem?

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1eb3984-4824c.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1eb3984-4824c.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?