Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show Hide a div in angular 7

I am trying to show/hide a div in angular 7 using ng-model and ng-hide but its not working.

button to Show/Hide- used ng-model to set expression

    <button type="checkbox" ng-model="show" >show/hide</button>

div to Show/Hide, Used ng-hide to hide the div

<div class="row container-fluid" ng-hide="show" id="divshow" >
  Div Content
</div>    
</body>
</html>

I have tried ng-model and ng-hide still it's not working. Please provide a solution

like image 740
Ayush Jain AJ Avatar asked Mar 27 '19 10:03

Ayush Jain AJ


People also ask

How do you do show hide in angular?

Copy # Angular element = false; We will use *ngIf to display and hide our div based on the condition. As you can see in the above example, we have set a condition if the element is true , the div will be displayed, and if the condition is false , it will not display.

How do I hide a div?

We hide the divs by adding a CSS class called hidden to the outer div called . text_container . This will trigger CSS to hide the inner div.

How do I toggle a div in angular 8?

In our application, we are going to use ngIf and hidden to toggle div on an event of button click. In our Angular application, we can use various versions of Angular such as Angular 6, 7, 8, 9, 10, and 11, to show, use, and hide toggle div on click event.

How do I show a div in TypeScript?

To hide/show an element in TypeScript, use the style. display property to determine if the element is shown or hidden. If its display property is set to none , show the element by setting it to block , otherwise hide the element by setting its display property to none .


Video Answer


2 Answers

In your HTML

<button (click)="toggleShow()" type="checkbox" >show/hide</button>

<div *ngIf="isShown" class="row container-fluid"  id="divshow" >
Div Content

</div>

in your component class add this:

isShown: boolean = false ; // hidden by default


toggleShow() {

this.isShown = ! this.isShown;

}
like image 145
Abderrahim Soubai-Elidrisi Avatar answered Oct 02 '22 14:10

Abderrahim Soubai-Elidrisi


Try this solution:

Solution 1:

<div *ngIf="show" class="row container-fluid"  id="divshow" >
        Div Content
    </div>

Solution 2:

<div [hidden]="!show" class="row container-fluid"  id="divshow" >
            Div Content
        </div>
like image 25
Seba Cherian Avatar answered Oct 02 '22 14:10

Seba Cherian