Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to toggle (show/hide) element in Angular 4?

Tags:

angular

This is my html element:

<ng-container>
    <span *ngIf="row.messageText && row.messageText.length >= 30 && expanded">{{row.messageText.substr(0, 25)}}
        <span>more</span>
    </span>
</ng-container>

It shows part of a message when row.messageText.length is greater than 30. I need the first span to show the entire message (by using row.messageText.substr()) after clicking the more span. I think that there is a solution by using (click)="toggle()" and setting some true/false values.

Do you have any ideas?

like image 408
bigmeister Avatar asked Nov 28 '17 10:11

bigmeister


2 Answers

In TS file

showMyContainer: boolean = false;

In HTML

<button (click)="showMyContainer=!showMyContainer">Show/Hide</button>

<div *ngIf="showMyContainer">
     your code
</div>

See my stackblitz

Happy to hear if someone got helped.

like image 143
Pullat Junaid Avatar answered Nov 13 '22 22:11

Pullat Junaid


<ng-container>
    <span *ngIf="row.messageText && row.messageText.length >= 30 && expanded == false">{{row.messageText.substr(0, 25)}}
        <span (click)="expanded = true">more</span>
    </span>
<span *ngIf="expanded == true">{{row.messageText}}</span>
</ng-container>

and set expanded = false initially in your ts file

like image 24
gaurav bankoti Avatar answered Nov 13 '22 21:11

gaurav bankoti