Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 [hidden] does not seem to be working?

Tags:

angular

My component is defined this way:

import { Component } from 'angular2/core'  @Component({     selector: 'sidebar',     templateUrl: 'js/app/views/sidebar.html', }) export class SidebarComponent {     public sections: boolean[] = [         true,         false,         false,         false,         false,         false,         false,         false     ]; } 

The sidebar.html template:

<h3 class="proofTitle">...</h3> <p [hidden]="sections[0]">     ... </p> <p [hidden]="sections[1]">     ... </p> ... <p [hidden]="sections[7]">     ... </p> 

Seems pretty straight forward, but for some reason, it's showing all of the sections... what am I missing?

like image 253
Serj Sagan Avatar asked Jan 07 '16 08:01

Serj Sagan


People also ask

How does hidden work in angular?

The DOM representation of the hidden attribute is a property also called hidden , which if set to true hides the element and false shows the element. Angular doesn't manipulate HTML attributes, it manipulates DOM properties because the DOM is what actually gets displayed.

How do you do show hide in angular?

AngularJS ng-hide Directive The ng-hide directive hides the HTML element if the expression evaluates to true. ng-hide is also a predefined CSS class in AngularJS, and sets the element's display to none .

How do you use ngIf instead of hidden?

ngIf will comment out the data if the expression is false. This way the data are not even loaded, causing HTML to load faster. [hidden] will load the data and mark them with the hidden HTML attribute. This way data are loaded even if they are not visible.

What is the difference between ngIf and hidden in angular?

The main difference between angular ngIf directive & hidden or display:none is ngIf will add or remove the element from DOM based on condition or expression. hidden attribute in html5 and display none CSS will show or hide the HTML element. We will go through the examples to understand them further.


2 Answers

Check that you don't have any display css rule on your <p> tags that would override the hidden behavior like:

p {   display: inline-block !important; } 

Because the hidden html attribute behaves like display: none

like image 73
bertrandg Avatar answered Sep 21 '22 12:09

bertrandg


Just add this code:

p[hidden] { display: none; } 

to your styles.css file.

like image 38
ranbuch Avatar answered Sep 20 '22 12:09

ranbuch