NG show/hide/if Examples
show/hide will create element but the element is in hidden state. ng-if element is created when ever it require, o/w removes element.
by Yashwanth M
HTML
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/foundation/5.2.2/css/foundation.min.css">
<div ng-app="app" ng-controller="LoginCtrl as user">
Show Me:
<input type = "checkbox" ng-model="Color"/>
<div ng-show = "Color" class="item" >
Show (ng-show)
</div><br/>
Hide Me:
<input type = "checkbox" ng-model="hideColor"/>
<div ng-hide = "hideColor" class="item" >
Show (ng-hide)
</div>
<br/><br/>
<div ng-if="!user.isLogged">
<p>Please Login</p>
<button ng-click="user.isLogged = true">Login</button>
</div>
<div ng-if="user.isLogged">
<p>Welcome !</p>
<button ng-click="user.isLogged = false">Logout</button>
</div>
</div>
SCSS
body{
//font-family: arial;
}
.item{
color: #fff;
padding: 20px;
background: #229EDC;
margin: 10px;
text-align: center;
}
button, .button {
border-style: solid;
border-width: 0px;
cursor: pointer;
font-family: "Helvetica Neue","Helvetica",Helvetica,Arial,sans-serif;
font-weight: normal;
line-height: normal;
margin: 0 0 1.25rem;
position: relative;
text-decoration: none;
text-align: center;
-webkit-appearance: none;
-webkit-border-radius: 0;
display: inline-block;
padding-top: 1rem;
padding-right: 2rem;
padding-bottom: 1.0625rem;
padding-left: 2rem;
font-size: 1rem;
background-color: #008cba;
border-color: #007095;
color: #fff;
transition: background-color 300ms ease-out;
}
JavaScript
angular.module('app', [])
.controller('LoginCtrl', function () {
this.isLogged = false;
});
/*
ng-show/ng-hide will always insert the DOM element, but will display/hide it based on the condition. ng-if will not insert the DOM element until the condition is not fulfilled.
ng-if is better when we needed the DOM to be loaded conditionally, as it will help load page bit faster compared to ng-show/ng-hide.
show/hide will create element but the element is in hidden state.
ng-if element is created when ever it require, o/w removes element.
*/