Angularjs Quick Start Guide 11: Events

Source: Internet
Author: User

ANGULARJS has its own HTML event directive.

Ng-click directive

  The ng-click directive defines the Angularjs click event.

<DivNg-app=""Ng-controller= "Myctrl"><ButtonNg-click= "Count = Count + 1">Click me!</Button><P>{{count}}</P></Div>

Run

Hide HTML elements

  The ng-hide directive can be used to display (or hide) Some of the content in the application.

  ng-hide= "True" will make the HTML element invisible(that is, hidden).

  ng-hide= "False" causes the HTML element to be visible (that is, viewable).

<DivNg-app= "MYAPP"Ng-controller= "Personctrl"><ButtonNg-click= "Toggle ()">Toggle</Button><PNg-hide= "MyVar">First Name:<inputtype= "text"Ng-model= "FirstName"><BR>Last Name:<inputtype= "text"Ng-model= "LastName"><BR><BR>Full Name: {{firstName + "" + LastName}}</P></Div><Script>varapp=Angular.module ('myApp', []); App.controller ('Personctrl', function($scope) {$scope. FirstName= "John", $scope. LastName= "Doe"$scope. MyVar= false; $scope. Toggle= function() {$scope. MyVar= !$scope. MyVar; };});</Script>

Run

Code Explanation:

  Personctrl defines a controller, which is the same as in the controller chapter.

Application has a default property (variable):$scope. MyVar = false;

Element <p> has two input tags, and theng-hide instruction sets the visibility by the value of the myval variable (TRUE or false).

The function Toggle () is used to convert the value of the myVar variable to TRUE or FALSE.

HTML elements are hidden (that is, invisible) when ng-hide= "true" .

displaying HTML elements

  The ng-show directive can also be used to display (or hide) portions of the application (as opposed to the Ng-hide Directive).

  ng-Show= "false" will make the HTML element invisible(that is, hidden).

  ng-Show= "true" will make the HTML element visible (that is, viewable).

The example in the previous section can also be used ng-show instead of implementing the same functionality:

<DivNg-app= "MYAPP"Ng-controller= "Personctrl"><ButtonNg-click= "Toggle ()">Toggle</Button><PNg-show= "MyVar">First Name:<inputtype= "text"Ng-model= "FirstName"><BR>Last Name:<inputtype= "text"Ng-model= "LastName"><BR><BR>Full Name: {{firstName + "" + LastName}}</P></Div><Script>varapp=Angular.module ('myApp', []); App.controller ('Personctrl', function($scope) {$scope. FirstName= "John", $scope. LastName= "Doe"$scope. MyVar= true; $scope. Toggle= function() {$scope. MyVar= !$scope. MyVar; }});</Script>

Run

Previous chapter-Angularjs Quick Start Guide 10:dom node Next chapter-Angularjs Quick Start Guide 12: Modules

Angularjs Quick Start Guide 11: Events

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.