AngularJS ng-model-options指令
ng-model-options指令的特点是帮助用户在当前应用程序中修改ngModel指令的行为。基本上,当用户需要控制范围内的变量和HTML表单元素的绑定时,就可以使用它。你还可以指定绑定发生时的等待时间。它可以被设定为需要一些时间,也可以几乎是即时完成。
语法:
<element ng-model-options="option"> </element>
参数:
- {updateOn: ‘event’}指定绑定应该在特定事件发生时发生。
- {debounce: 1000}指定与绑定的等待时间,单位为毫秒。
- {allowInvalid: true|false}指定如果值没有被验证,是否可以进行绑定。
- {getterSetter : true|false}指定绑定到模型的函数是否应被视为getter/setters。
- {时间区:’0100′}指定在处理Date对象时应该使用哪个时区。
例子1:这个例子将告诉你如何通过_ng-model-options指令来保持一个输入字段的数据绑定,直到该字段的焦点消失。你也会注意到,当你输入一些东西时,值会立即被更新。
<!DOCTYPE html>
<html>
<head>
<script src="
https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">
</script>
</head>
<body style="text-align:center">
<h1 style="color:green">
GeeksforGeeks
</h1>
<h3 style="color:purple">
ng-model-options directive
</h3>
<div ng-app="myApp" ng-controller="myCtrl">
<p>Please enter something below:</p>
<input ng-model="name" ng-model-options="{
updateOn: 'default blur',
debounce: { default: 500, blur: 0 } }"
style="text-align:center">
<p>
The binding is going to wait for
the value until the focus of the
field is lost:
</p>{{name}}
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function (scope) {
scope.name = "Data Structures & Algorithms";
});
</script>
</body>
</html>
输出:
示例2:
<!DOCTYPE html>
<html>
<head>
<script src="
https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">
</script>
</head>
<body style="text-align:center">
<h1 style="color:green">
GeeksforGeeks
</h1>
<h3 style="color:purple">
ng-model-options Directive
</h3>
<div ng-app="myApp" ng-controller="myCtrl">
<p>Enter some text:</p>
<textarea ng-model="name" ng-model-options="{
updateOn: 'default blur',
debounce: { default: 500, blur: 0 } }">
</textarea>
<p>
The binding is going to wait for
the value until the focus of the
field is lost:
</p>{{name}}
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function (scope) {
scope.name =
"GeeksforGeeks Learning Together!"
});
</script>
</body>
</html>
输出: