Just add a click event onto the button, using (click)="callback()"
When it's clicked, inside of the callback
method, toggle a class property between true/false.
public expanded = false;
callback() {
this.expanded = !this.expanded;
}
On your div that you want to toggle, toggle a class:
<div [ngClass]="{'expanded': expanded}"></div>
This will add the class 'expanded' when the class property 'expanded' is true. Then add appropriate CSS styling to change the height based on what you want it to look like.
So clicking the button will now toggle the class property expanded between true and false, which in turn will toggle the css class on the div.
You can use ngClass
to toggle a class like unexpand
and this class set the height of the div
style
.unexpand {
height:100px;
overflow: hidden;
}
template
<div [ngClass]="{unexpand:isUnexpand}" >
...
</div>
<button (click)="isUnexpand = !isUnexpand">
{{isUnexpand ? 'Expand' :'Unexpand'}}
</button>
compoenent
isUnexpand:boolean=false
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.