Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 5 ngHide ngShow [hidden] not working

Good day, guys!

I am trying to make my Angular 5 app hide elements (or show hidden). However, this seems to not work.

I've tried ngHide, ng-hide, ngShow, ng-show, [hidden] methods - none of them works.

My login.component.ts looks like this:

import { Component, OnInit } from '@angular/core';  @Component({   selector: 'login',   templateUrl: './login.component.html',   styleUrls: ['./login.component.css'] })  export class LoginComponent implements OnInit {   isHidden: boolean = true;   input_pw: string = 'password';    constructor() { }    ngOnInit() {     console.log(this.isHidden); //console shows true   }  } 

And login.component.html:

<div class="container">    <div class="cont-login">     <div class="login-pane">        <div>         <p class="inner log-labels">Your password</p>         <input class="txt" type="password" [(ngModel)]="input_pw" ngHide="isHidden">       </div>        <div>         <input class="btn" type="submit" value="Login">       </div>      </div>     </div>  </div> 

Am I doing something wrong here?

NOTE: I did not change or add anything related to css.

like image 574
zilijonas Avatar asked Apr 05 '18 10:04

zilijonas


People also ask

What is the equivalent of ngShow and ngHide in angular?

You can use [style. display]="'block'" to replace ngShow and [style. display]="'none'" to replace ngHide.

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.

What is the difference between ngIf and 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.

Is hidden HTML?

The hidden global attribute is a Boolean attribute indicating that the element is not yet, or is no longer, relevant. For example, it can be used to hide elements of the page that can't be used until the login process has been completed. Browsers won't render elements with the hidden attribute set.


1 Answers

If you want to just toggle visibility and still keep the input in DOM:

<input class="txt" type="password" [(ngModel)]="input_pw"   [style.visibility]="isHidden? 'hidden': 'visible'"> 

The other way around is as per answer by rrd, which is to use HTML hidden attribute. In an HTML element if hidden attribute is set to true browsers are supposed to hide the element from display, but the problem is that this behavior is overridden if the element has an explicit display style mentioned.

.hasDisplay {    display: block;  }
<input class="hasDisplay" hidden value="shown" />  <input hidden value="not shown">

To overcome this you can opt to use an explicit css for [hidden] that overrides the display;

[hidden] {   display: none !important; } 

Yet another way is to have a is-hidden class and do:

<input [class.is-hidden]="isHidden"/>  .is-hidden {       display: none;  } 

If you use display: none the element will be skipped from the static flow and no space will be allocated for the element, if you use visibility: hidden it will be included in the flow and a space will be allocated but it will be blank space.

The important thing is to use one way across an application rather than mixing different ways thereby making the code less maintainable.

If you want to remove it from DOM

<input class="txt" type="password" [(ngModel)]="input_pw" *ngIf="!isHidden"> 
like image 87
sabithpocker Avatar answered Sep 17 '22 15:09

sabithpocker