EVOLUTION-MANAGER
Edit File: ngModelOptions.html
<a href='https://github.com/angular/angular.js/edit/v1.3.x/src/ng/directive/ngModel.js?message=docs(ngModelOptions)%3A%20describe%20your%20change...#L1063' class='improve-docs btn btn-primary'><i class="glyphicon glyphicon-edit"> </i>Improve this Doc</a> <a href='https://github.com/angular/angular.js/tree/v1.3.9/src/ng/directive/ngModel.js#L1063' class='view-source pull-right btn btn-primary'> <i class="glyphicon glyphicon-zoom-in"> </i>View Source </a> <header class="api-profile-header"> <h1 class="api-profile-header-heading">ngModelOptions</h1> <ol class="api-profile-header-structure naked-list step-list"> <li> - directive in module <a href="api/ng">ng</a> </li> </ol> </header> <div class="api-profile-description"> <p>Allows tuning how model updates are done. Using <code>ngModelOptions</code> you can specify a custom list of events that will trigger a model update and/or a debouncing delay so that the actual update only takes place when a timer expires; this timer will be reset after another change takes place.</p> <p>Given the nature of <code>ngModelOptions</code>, the value displayed inside input fields in the view might be different than the value in the actual model. This means that if you update the model you should also invoke <a href="api/ng/type/ngModel.NgModelController"><code>$rollbackViewValue</code></a> on the relevant input field in order to make sure it is synchronized with the model and that any debounced action is canceled.</p> <p>The easiest way to reference the control's <a href="api/ng/type/ngModel.NgModelController"><code>$rollbackViewValue</code></a> method is by making sure the input is placed inside a form that has a <code>name</code> attribute. This is important because <code>form</code> controllers are published to the related scope under the name in their <code>name</code> attribute.</p> <p>Any pending changes will take place immediately when an enclosing form is submitted via the <code>submit</code> event. Note that <code>ngClick</code> events will occur before the model is updated. Use <code>ngSubmit</code> to have access to the updated model.</p> <p><code>ngModelOptions</code> has an effect on the element it's declared on and its descendants.</p> </div> <div> <h2>Directive Info</h2> <ul> <li>This directive executes at priority level 0.</li> </ul> <h2 id="usage">Usage</h2> <div class="usage"> <ul> <li>as attribute: <pre><code><ANY ng-model-options=""> ... </ANY></code></pre> </li> </div> <section class="api-section"> <h3>Arguments</h3> <table class="variables-matrix input-arguments"> <thead> <tr> <th>Param</th> <th>Type</th> <th>Details</th> </tr> </thead> <tbody> <tr> <td> ngModelOptions </td> <td> <a href="" class="label type-hint type-hint-object">Object</a> </td> <td> <p>options to apply to the current model. Valid keys are:</p> <ul> <li><code>updateOn</code>: string specifying which event should the input be bound to. You can set several events using an space delimited list. There is a special event called <code>default</code> that matches the default events belonging of the control.</li> <li><code>debounce</code>: integer value which contains the debounce model update value in milliseconds. A value of 0 triggers an immediate update. If an object is supplied instead, you can specify a custom value for each event. For example: <code>ng-model-options="{ updateOn: 'default blur', debounce: {'default': 500, 'blur': 0} }"</code></li> <li><code>allowInvalid</code>: boolean value which indicates that the model can be set with values that did not validate correctly instead of the default behavior of setting the model to undefined.</li> <li><code>getterSetter</code>: boolean value which determines whether or not to treat functions bound to <code>ngModel</code> as getters/setters.</li> <li><code>timezone</code>: Defines the timezone to be used to read/write the <code>Date</code> instance in the model for <code><input type="date"></code>, <code><input type="time"></code>, ... . Right now, the only supported value is <code>'UTC'</code>, otherwise the default timezone of the browser will be used.</li> </ul> </td> </tr> </tbody> </table> </section> <h2 id="example">Example</h2><p>The following example shows how to override immediate updates. Changes on the inputs within the form will update the model only when the control loses focus (blur event). If <code>escape</code> key is pressed while the input field is focused, the value is reset to the value in the current model.</p> <p> <div> <a ng-click="openPlunkr('examples/example-ngModelOptions-directive-blur')" class="btn pull-right"> <i class="glyphicon glyphicon-edit"> </i> Edit in Plunker</a> <div class="runnable-example" path="examples/example-ngModelOptions-directive-blur" name="ngModelOptions-directive-blur" module="optionsExample"> <div class="runnable-example-file" name="index.html" language="html" type="html"> <pre><code><div ng-controller="ExampleController"> <form name="userForm"> Name: <input type="text" name="userName" ng-model="user.name" ng-model-options="{ updateOn: 'blur' }" ng-keyup="cancel($event)" /><br /> Other data: <input type="text" ng-model="user.data" /><br /> </form> <pre>user.name = <span ng-bind="user.name"></span></pre> </div></code></pre> </div> <div class="runnable-example-file" name="app.js" language="js" type="js"> <pre><code>angular.module('optionsExample', []) .controller('ExampleController', ['$scope', function($scope) { $scope.user = { name: 'say', data: '' }; $scope.cancel = function(e) { if (e.keyCode == 27) { $scope.userForm.userName.$rollbackViewValue(); } }; }]);</code></pre> </div> <div class="runnable-example-file" name="protractor.js" type="protractor" language="js"> <pre><code>var model = element(by.binding('user.name')); var input = element(by.model('user.name')); var other = element(by.model('user.data')); it('should allow custom events', function() { input.sendKeys(' hello'); input.click(); expect(model.getText()).toEqual('say'); other.click(); expect(model.getText()).toEqual('say hello'); }); it('should $rollbackViewValue when model changes', function() { input.sendKeys(' hello'); expect(input.getAttribute('value')).toEqual('say hello'); input.sendKeys(protractor.Key.ESCAPE); expect(input.getAttribute('value')).toEqual('say'); other.click(); expect(model.getText()).toEqual('say'); });</code></pre> </div> <iframe class="runnable-example-frame" src="examples/example-ngModelOptions-directive-blur/index.html" name="example-ngModelOptions-directive-blur"></iframe> </div> </div> </p> <p> This one shows how to debounce model changes. Model will be updated only 1 sec after last change. If the <code>Clear</code> button is pressed, any debounced action is canceled and the value becomes empty.</p> <p> <div> <a ng-click="openPlunkr('examples/example-ngModelOptions-directive-debounce')" class="btn pull-right"> <i class="glyphicon glyphicon-edit"> </i> Edit in Plunker</a> <div class="runnable-example" path="examples/example-ngModelOptions-directive-debounce" name="ngModelOptions-directive-debounce" module="optionsExample"> <div class="runnable-example-file" name="index.html" language="html" type="html"> <pre><code><div ng-controller="ExampleController"> <form name="userForm"> Name: <input type="text" name="userName" ng-model="user.name" ng-model-options="{ debounce: 1000 }" /> <button ng-click="userForm.userName.$rollbackViewValue(); user.name=''">Clear</button><br /> </form> <pre>user.name = <span ng-bind="user.name"></span></pre> </div></code></pre> </div> <div class="runnable-example-file" name="app.js" language="js" type="js"> <pre><code>angular.module('optionsExample', []) .controller('ExampleController', ['$scope', function($scope) { $scope.user = { name: 'say' }; }]);</code></pre> </div> <iframe class="runnable-example-frame" src="examples/example-ngModelOptions-directive-debounce/index.html" name="example-ngModelOptions-directive-debounce"></iframe> </div> </div> </p> <p> This one shows how to bind to getter/setters:</p> <p> <div> <a ng-click="openPlunkr('examples/example-ngModelOptions-directive-getter-setter')" class="btn pull-right"> <i class="glyphicon glyphicon-edit"> </i> Edit in Plunker</a> <div class="runnable-example" path="examples/example-ngModelOptions-directive-getter-setter" name="ngModelOptions-directive-getter-setter" module="getterSetterExample"> <div class="runnable-example-file" name="index.html" language="html" type="html"> <pre><code><div ng-controller="ExampleController"> <form name="userForm"> Name: <input type="text" name="userName" ng-model="user.name" ng-model-options="{ getterSetter: true }" /> </form> <pre>user.name = <span ng-bind="user.name()"></span></pre> </div></code></pre> </div> <div class="runnable-example-file" name="app.js" language="js" type="js"> <pre><code>angular.module('getterSetterExample', []) .controller('ExampleController', ['$scope', function($scope) { var _name = 'Brian'; $scope.user = { name: function(newName) { return angular.isDefined(newName) ? (_name = newName) : _name; } }; }]);</code></pre> </div> <iframe class="runnable-example-frame" src="examples/example-ngModelOptions-directive-getter-setter/index.html" name="example-ngModelOptions-directive-getter-setter"></iframe> </div> </div> </p> </div>