AngularJS ng-model directive
2016-04-03 21:28
375 查看
with the ng-model directive youan bind the value of an input field
to a varible
TWO_WAT binding
1.by script
<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>
Try it Yourself »
2. by expresson
<div ng-app="myApp" ng-controller="myCtrl">
Name: <input ng-model="name">
<h1>You entered: {{name}}</h1>
</div>
Try it Yourself »
Validae User Input:
<form ng-app="" name="myForm" ng-init="myText
= 'post@myweb.com'">
Email:
<input type="email" name="myAddress" ng-model="myText"required>
<h1>Status</h1>
{{myForm.myAddress.$valid}}
{{myForm.myAddress.$dirty}}
{{myForm.myAddress.$touched}}
</form>
Try it Yourself »
Application Status:
ng-empty
ng-not-empty
ng-touched
ng-untouched
ng-valid
ng-invalid
ng-dirty
ng-pending
ng-pristine
<form ng-app="" name="myForm">
Email:
<input type="email" name="myAddress" ng-model="text">
<span ng-show="myForm.myAddress.$error.email">Not
a valid e-mail address</span>
</form>
Try it Yourself »
The ng-model directive provide CSS classes for HTML elements,
depending on their status
<style>
input.ng-invalid {
background-color: lightblue;
}
</style>
<body>
<form ng-app="" name="myForm">
Enter your name:
<input name="myAddress" ng-model="text" required>
</form>
Try it Yourself »
in the above case, we don't give input tag any type such as "number" or "email" ,and that means only if the input not is empty, the input status will keep valid.
to a varible
TWO_WAT binding
1.by script
Example
<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>
Try it Yourself »
2. by expresson
Example
<div ng-app="myApp" ng-controller="myCtrl">Name: <input ng-model="name">
<h1>You entered: {{name}}</h1>
</div>
Try it Yourself »
Validae User Input:
Example
<form ng-app="" name="myForm" ng-init="myText= 'post@myweb.com'">
Email:
<input type="email" name="myAddress" ng-model="myText"required>
<h1>Status</h1>
{{myForm.myAddress.$valid}}
{{myForm.myAddress.$dirty}}
{{myForm.myAddress.$touched}}
</form>
Try it Yourself »
Application Status:
ng-empty
ng-not-empty
ng-touched
ng-untouched
ng-valid
ng-invalid
ng-dirty
ng-pending
ng-pristine
Example
<form ng-app="" name="myForm">Email:
<input type="email" name="myAddress" ng-model="text">
<span ng-show="myForm.myAddress.$error.email">Not
a valid e-mail address</span>
</form>
Try it Yourself »
The ng-model directive provide CSS classes for HTML elements,
depending on their status
Example
<style>input.ng-invalid {
background-color: lightblue;
}
</style>
<body>
<form ng-app="" name="myForm">
Enter your name:
<input name="myAddress" ng-model="text" required>
</form>
Try it Yourself »
in the above case, we don't give input tag any type such as "number" or "email" ,and that means only if the input not is empty, the input status will keep valid.
相关文章推荐
- angularJs小应用----计算购物金额-动态改变邮费
- 初识angularJs
- Angular 根据 service 的状态更新 directive
- AngularJs 60分钟入门基础教程
- AngularJs 基础(60分钟入门) (转)
- 初识angularJs
- Angularjs学习笔记1_基本技巧
- AngularJs 60分钟入门基础教程
- Angular 根据 service 的状态更新 directive
- angularJs directives
- angularjs介绍
- AngularJS专题——路由
- Angularjs动态绑定HTML文本
- 详说Angular之指令(directive)
- angularjs
- 【技术干货】测试Angular项目的正确姿势
- 21、AngularJs知识点总结 part-3
- angularjs学习笔记之directive的返回对象属性(一)
- 火狐浏览器angular跳转layer弹窗兼容问题
- angularJS select