Skip to content

Instantly share code, notes, and snippets.

@ndamnjanovic
Created January 21, 2014 09:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ndamnjanovic/8536886 to your computer and use it in GitHub Desktop.
Save ndamnjanovic/8536886 to your computer and use it in GitHub Desktop.
angular - conditionally displaying of html blocks
In AngularJS, you have several ways to display/hide some HTML block.
I'll mention most common one, and point out main differencies between them.
1. ng-hide/ng-show: By using these two directives, you can hide or show html block, by simple passing expression. Example:
<div ng-hide='checked' > Hide me, if checked is true </div>
2. another way of doing same thing, is to use ng-class. You can conditionally apply some class to HTML block, and by doing that, hide or show it.
<div ng-class="{'hidden': isSomethingTrue()}"> Hide me, if Something is true </div>
You can use ng-class as attribute, or as a class.
3. Using ng-if. IMPORTANT NOTE: Main difference between ng-if and two cases above (when speaking about hiding HTML blocks), is that ng-if will not render HTML block if condition is not satisfied, while two above will render (but show hidden).
<div ng-if="isThisVisible">
Display me only if condition is met (isThisVisible is true)
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment