Angularjs Model Detailed Introduction and example code _ANGULARJS

Source: Internet
Author: User
Tags valid email address


Angularjs Ng-model Instruction



The Ng-model directive is used to bind application data to the value of the HTML controller (input, select, textarea).



Ng-model directives



The Ng-model directive can bind the value of an input field to a variable created by ANGULARJS.



Angularjs instance

<! DOCTYPE html>
<html>
<head>
<meta charset = "utf-8">
<script src = "http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"> </ script>
</ head>
<body>

<div ng-app = "myApp" ng-controller = "myCtrl">
Name: <input ng-model = "name">
</ div>

<script>
var app = angular.module ('myApp', []);
app.controller ('myCtrl', function ($ scope) {
  $ scope.name = "John Doe";
});
</ script>

<p> Use the ng-model directive to bind the value of the input field to the properties of the controller. </ p>

</ body>
</ html>
operation result:

first name: 
John Doe
Use the ng-model directive to bind the value of the input field to the properties of the controller.

Two-way binding
Two-way binding, when the value of the input field is modified, the value of the AngularJS property will also be modified:
AngularJS example

<! DOCTYPE html>
<html>
<head>
<meta charset = "utf-8">
<script src = "http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"> </ script>
</ head>
<body>

<div ng-app = "myApp" ng-controller = "myCtrl">
Name: <input ng-model = "name">
<h1> You entered: {{name}} </ h1>
</ div>

<script>
var app = angular.module ('myApp', []);
app.controller ('myCtrl', function ($ scope) {
  $ scope.name = "John Doe";
});
</ script>

<p> Modify the value of the input box, and the title name will be modified accordingly. </ p>

</ body>
</ html>
operation result:

first name: 
John Doe

You type: John Doe

Modify the value of the input box, the title of the title will be modified accordingly.

Verify user input
AngularJS example

<! DOCTYPE html>
<html>
<head>
<meta charset = "utf-8">
<script src = "http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"> </ script>
</ head>
<body>

<form ng-app = "" name = "myForm">
  Email:
  <input type = "email" name = "myAddress" ng-model = "text">
  <span ng-show = "myForm.myAddress. $ error.email"> is not a valid email address </ span>
</ form>

<p> Enter your email address in the input box. If it is not a valid email address, a prompt message will pop up. </ p>

</ body>
</ html>
operation result:

Email:

Enter your email address in the input box. If it is not a valid email address, a prompt message will pop up.

 CSS class
The ng-model directive provides CSS classes for HTML elements based on their state:
AngularJS example

<! DOCTYPE html>
<html>
<head>
<meta charset = "utf-8">
<script src = "http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"> </ script>
<style>
input.ng-invalid {
  background-color: lightblue;
}
</ style>
</ head>
<body>

<form ng-app = "" name = "myForm">
  Enter your name:
  <input name = "myName" ng-model = "myText" required>
</ form>

<p> Edit the text field, the background color will be changed in different states. </ p>
<p> The required attribute is added to the text field. This value is required. If it is empty, it is illegal. </ p>

</ body>
</ html>
 operation result:

Enter your name:
Edit the text field, the background color will be changed in different states.

The required attribute is added to the text field. This value is required. If it is empty, it is illegal.

The ng-model directive adds / removes the following classes based on the state of the form field:
ng-empty
ng-not-empty
ng-touched
ng-untouched
ng-valid
ng-invalid
ng-dirty
ng-pending
ng-pristine

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.