AngularJS changes the element display status, while angularjs changes the element

Source: Internet
Author: User

AngularJS changes the element display status, while angularjs changes the element

Preface

This article describes how to use the ng-show and ng-hide commands provided by AngularJS to automatically listen to a Boolean variable to change the element display status.

There are n methods to control the display and hiding of html elements: html hidden, css display, jQuery hide (), show (), and bootstrap. hide. The focus of today is not to show and hide, but to listen to a Boolean variable value and automatically change the element display and hide states. Listening function, if judgment, dom selection, dom setting, five lines of code are not feasible, and there is no technical content.

Instance 1

<Body> <div ng-controller = "VisibleController"> <p ng-show = "visible"> string 1 </p> <p ng-hide = "visible"> string 2 </p> <button ng-click = "toggle () "> switch </button> </div> <script src = ".. /lib/angularjs/1.2.26/angular. min. js "> </script> <script> function VisibleController ($ scope) {$ scope. visible = false; $ scope. toggle = function () {$ scope. visible =! $ Scope. visible ;}}</script> </body>

The two commands are simple. They are only displayed when ng-show is true, hidden when false, and opposite to ng-hide.

For menus, context-sensitive tools, and many other cases, displaying and hiding elements is a core function. Like other functions in Angualr,Angular is used to drive UI refresh by modifying the data model.And then the change is reflected to the UI through commands.

The functions of ng-show and ng-hide commands are equivalent, but the running effect is the opposite. We can display or hide elements based on the passed expressions. That is to say, when the expression ng-show is true, the element is displayed. If the expression is false, the element is hidden, while ng-hide is the opposite.

Working Principle

The operating principle of these two commands is: Set the element style to display the element according to the actual situation, and set it to display: none to hide the element.

Instance 2

<body ng-controller='ShowController'> <button ng-click="toggleMenu()">Toggle Menu</button> <ul ng-show='menuState.show'> <li>Stun</li> <li>Disintegrate</li> <li>Erase from history</li> </ul>  <script src="lib/angular/angular.js"></script> <script> var myApp=angular.module('myApp',[]) myApp.controller('ShowController',function($scope)  {$scope.menuState={show: false},$scope.toggleMenu=function() {$scope.menuState.show=!$scope.menuState.show;}}); </script> </body> 

The above is all the content of this article. I hope this article will help you in your study or work. I also hope to provide more support to the customer's home!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.