Angularjs之ngModel中的值验证绑定方法
众所周知,在Angular中ngModel为动态双向绑定,存在两种方式。
例如,
方式一:
在html中,
<input type="text" ng-model="searchText" />
<button ng-click="check(searchText)">Check!</button>
{{ searchText }}
在controller中
$scope.check = function (searchText) {
console.log(searchText);
}
方式二:
引用stackoverflow的一句话,
“If you use ng-model, you have to have a dot in there.” Make your model point to an object.property and you'll be good to go.
在html中,
<input ng-model="formData.searchText"/> <button ng-click="check()">Check!</button>
在controller中,
$scope.formData = {};
$scope.check = function () {
console.log($scope.formData.searchText.$modelValue);
}
但是我们常常会对ngModel当中的值进行验证,例如,
<input type="password" pattern="[0-9]*" placeholder="请输入6位新密码" ng-model="password.new_password" ng-blur="validateLength()">
<input type="password" ng-model="password.new_password" ng-keyup="compare(password)" name="repassword" ng-pattern="/^[0-9]{1,6}$/" />
发现ngModel当中的password.new_password不能实时绑定到controller中,原因为ngModel的值并没有符合pattern的规则。若符合规则,则正常绑定传递。
以上这篇Angularjs之ngModel中的值验证绑定方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
AngularJS的ng Http Request与response格式转换方法
这篇文章主要介绍了AngularJS的ng Http Request与response格式转换方法,结合实例形式分析了AngularJS实现Request与response格式转换操作的相关设置与使用技巧,需要的朋友可以参考下2016-11-11
AngularJS入门教程之 XMLHttpRequest实例讲解
本文主要讲解 AngularJS XMLHttpRequest,这里给大家整理相关资料并提供实例代码,有需要的小伙伴参考下2016-07-07
Angular使用 ng-img-max 调整浏览器中的图片的示例代码
本篇文章主要介绍了Angular使用 ng-img-max 调整浏览器中的图片的示例代码,具有一定的参考价值,有兴趣的可以了解一下2017-08-08
AngularJS实现与Java Web服务器交互操作示例【附demo源码下载】
这篇文章主要介绍了AngularJS实现与Java Web服务器交互操作的方法,结合实例形式较为详细的分析了AngularJS前台ajax提交与javascript后台处理的完整流程与实现技巧,并附带demo源码供读者下载参考,需要的朋友可以参考下2016-11-11
Angular js 实现添加用户、修改密码、敏感字、下拉菜单的综合操作方法
这篇文章主要介绍了Angular js 实现添加用户、修改密码、敏感字、下拉菜单的综合操作方法,需要的朋友可以参考下2017-10-10


最新评论