3

I was going through the Angular 2 documentation and found the following:

<div [hidden]="name.valid || name.pristine"
 class="alert alert-danger">

There is no explanation on the [hidden] part. Is it a HTML property, CSS or Angular? Why isn't ng-show used instead?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Sarah cartenz
  • 1,313
  • 2
  • 14
  • 37

2 Answers2

4

ng-show is a history of angular1. Before there was no hidden property (standard HTML, it is available starting from ie11) also all standard properties were covered by some similar angular directives which were doing the stuff.

This was a problem because it forced angular programmers to learn lots of directives.

Angular2 idea was to remove all unnecessary directives, and ng-show was one of them. Instead, angular2 introduced a property binding [hidden] which does the same job.

So the answer is: this is standard HTML property utilized by the angular2 property binding.

smnbbrv
  • 23,502
  • 9
  • 78
  • 109
2

hidden is a global HTML attribute. The square brackets indicate a property binding. Therefore

[hidden]="name.valid || name.pristine"

means "apply the hidden attribute to this element if the specified control is either valid or pristine". The browser will then render it but with display: none.

You could alternatively use *ngIf to remove the element from the rendered DOM entirely, see e.g. this answer for a comparison.

It's unclear why you reference ng-show, which was part of Angular 1 and no longer exists in 2.

Community
  • 1
  • 1
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437