Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

feat(input): Allow custom events and timeouts to trigger model updates #2129

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions angularFiles.js
Expand Up @@ -63,6 +63,7 @@ angularFiles = {
'src/ng/directive/ngStyle.js',
'src/ng/directive/ngSwitch.js',
'src/ng/directive/ngTransclude.js',
'src/ng/directive/ngUpdateModel.js',
'src/ng/directive/script.js',
'src/ng/directive/select.js',
'src/ng/directive/style.js'
Expand Down
40 changes: 40 additions & 0 deletions docs/content/guide/forms.ngdoc
Expand Up @@ -180,6 +180,46 @@ This allows us to extend the above example with these features:
</example>


# Non-immediate (debounced) or custom triggered model updates

By default, any change on the content will trigger a model update and form validation. You can override this behavior using the `ng-update-model-on`
attribute to bind only to a comma-delimited list of events. I.e. `ng-update-model-on="blur"` will update and validate only after the control loses
focus.

If you want to keep the default behavior and just add new events that may trigger the model update
and validation, add "default" as one of the specified events. I.e. `ng-update-model-on="default,mousedown"`

You can delay the model update/validation using `ng-update-model-debounce`. I.e. `ng-update-model-debounce="500"` will wait for half a second since
the last content change before triggering the model update and form validation. This debouncing feature is not available on radio buttons.

Custom debouncing timeouts can be set for each event for each event if you use an object in `ng-update-model-on`.
I.e. `ng-update-model-on="{default: 500, blur: 0}"`

Using the object notation allows any valid Angular expression to be used inside, including data and function calls from the scope.

If those attributes are added to an element, they will be applied to all the child elements and controls that inherit from it unless they are
overriden.

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).

<doc:example>
<doc:source>
<div ng-controller="ControllerUpdateOn">
<form name="form" class="css-form" ng-update-model-on="blur">
Name:
<input type="text" ng-model="user.name" name="uName" /><br />
</form>
<pre>model = {{user | json}}</pre>
</div>
<script>
function ControllerUpdateOn($scope) {
$scope.user = {};
}
</script>
</doc:source>
</doc:example>


# Custom Validation

Expand Down
4 changes: 4 additions & 0 deletions src/AngularPublic.js
Expand Up @@ -48,6 +48,8 @@
ngValueDirective,
ngAttributeAliasDirectives,
ngEventDirectives,
ngUpdateModelOnDirective,
ngUpdateModelDebounceDirective,

$AnchorScrollProvider,
$AnimateProvider,
Expand Down Expand Up @@ -183,6 +185,8 @@ function publishExternalAPI(angular){
ngChange: ngChangeDirective,
required: requiredDirective,
ngRequired: requiredDirective,
ngUpdateModelOn: ngUpdateModelOnDirective,
ngUpdateModelDebounce: ngUpdateModelDebounceDirective,
ngValue: ngValueDirective
}).
directive({
Expand Down