AngularJs ng-change事件/指令
2016-09-17 17:53
597 查看
定义和用法
ng-change 指令用于告诉 AngularJS 在 HTML 元素值改变时需要执行的操作。
ng-change 指令需要搭配
AngularJS ng-change 指令指令不会覆盖原生的 onchange 事件, 如果触发该事件,ng-change 表达式与原生的 onchange 事件都会执行。
ng-change 事件在值的每次改变时触发,它不需要等待一个完成的修改过程,或等待失去焦点的动作。
ng-change 事件只针对输入框值的真实修改,而不是通过 JavaScript 来修改。
<input>, <select>, 和 <textarea> 元素支持。
<radio>,<checkbox>
HTML
ng-change 指令用于告诉 AngularJS 在 HTML 元素值改变时需要执行的操作。
ng-change 指令需要搭配
ng-model指令使用。
AngularJS ng-change 指令指令不会覆盖原生的 onchange 事件, 如果触发该事件,ng-change 表达式与原生的 onchange 事件都会执行。
ng-change 事件在值的每次改变时触发,它不需要等待一个完成的修改过程,或等待失去焦点的动作。
ng-change 事件只针对输入框值的真实修改,而不是通过 JavaScript 来修改。
语法
<element ng-change="expression"></element><input>, <select>, 和 <textarea> 元素支持。
<radio>,<checkbox>
参数值
值 | 描述 |
---|---|
expression | 元素值改变时执行表达式。 |
实例说明,radio和checkbox
注:checkbox ng-model总是是true或false,而不是value,其他的ng-model默认都是value 的值HTML
<h3>Radio 控件测试</h3> <p><label> <input type="radio" value="男" name="sex" ng-model="value1" ng-change="radioChecked()" /> 男 </label> <label> <input type="radio" value="女" name="sex" ng-model="value1" ng-change="radioChecked()" /> 女 </label></p> <h3>checked 控件测试</h3> <p><div class="checkbox"> <label> <input name="agree" type="checkbox" value="同意" ng-model="value2" ng-change="checkboxClick()" /> 同意协议 </label> </div> <div class="checkbox"> <label> <input name="agree" type="checkbox" value="同意2" ng-model="value2" ng-change="checkboxClick()" /> 同意协议2 </label> </div></p>JS:
var app = angular.module('myApp', []); app.controller('validateCtrl', function ($scope) { //randio ng-change事件和原始onchange相同 //radio ng-model 的值是value $scope.radioChecked = function () { console.info($scope.value1); } //checkbox ng-change事件和原始onchange相同 //checkbox ng-model总是是true或false $scope.checkboxClick = function () { console.info($scope.value2); } });
实例说明,text,select
HTML<form class="form-horizontal"> <div class="form-group"> <label class="control-label">姓名:</label> <input type="text" class="form-control" ng-model="name" ng-change="txtChange();" /> </div> <div class="form-group"> <label class="control-label">选择年级:</label> <select class="form-control" ng-change="selectChange();" ng-model="grade"> <option value="1">一年级</option> <option value="2">二年级</option> </select> </div> </form>JS
var app = angular.module('myApp', []); app.controller('validateCtrl', function ($scope) { //textbox 的ng-change事件和原始ng-change不相同,而是和$scope.$watch()监听相同 //textbox 的ng-model为当前输入框的内容,为value值 $scope.txtChange = function () { console.info($scope.name); } //select 的ng-change事件和原始ng-change相同 //ng-model 的默认情况下ng-model的结果为value值 $scope.selectChange = function () { console.info($scope.grade); } });
相关文章推荐
- AngularJS之指令
- angularjs中ng-show的使用
- AngularJS使用自定义指令替代ng-repeat的方法
- AngularJS中关于ng-class指令的几种实现方式详解
- AngularJS中过滤器的使用与自定义实例代码
- Angular定义服务-Learn By Doing
- angularjs中使用$scope.$watch监控对象模型的变化
- Angular2学习笔记——NgModule
- [Angular2] 升级到Angular 2 final正式版
- angularjs中的$interpolate服务
- (转载)AngularJS开发人员最常犯的10个错误
- AngularJS 2.0 稳定版真的发布了!
- AngularJS 2.0 稳定版真的发布了!
- 详解Angular2中的编程对象Observable
- [Angular 2] Understanding @Injectable
- [Angular 2] Factory Provider with dependencies
- AngularJS中过滤器的使用与自定义实例代码
- AngularJS中关于ng-class指令的几种实现方式详解
- AngularJS使用自定义指令替代ng-repeat的方法
- [Angular 2] Factory Provider