Angular has a number of options for delaying the update to a model based on events in an input field:
<input type="text" name="username"
ng-model="user.name"
ng-model-options="{updateOn: 'default blur',
debounce: {default: 500, blur: 0} }" />
(See ng-model options)
The basic options that Angular offers allows the editing of an input form to be decoupled from the actual update of the model. There are some nice bonuses with this in that we can roll the value of the object back if we decide to cancel that update.
However, I would like to delay the update of the model, not based on an event on the input field but instead based on someone clicking a submit button.
I would ideally like a form where the user can update the inputs (let's say for a .user
form, any bound elements in the page {{user.first_name}}
etc. do not update and then when the user clicks submit those updates get committed to the model.
Equally if the user clicks "Cancel" the updates can all be rolled back.
<input type="text" name="username"
ng-model="user.name"
ng-model-options="{updateOn: 'myCustomEvent' }" />
<button onClick="broadCastMyCustomEventIntoForm()">Save changes</>
Is this possible?
ngModel usually use for input tags for bind a variable that we can change variable from controller and html page but ngBind use for display a variable in html page and we can change variable just from controller and html just show variable. Save this answer.
By setting the ng-model directive on an input field, we can use the value of the input field as an expression in a filter.
The ngModel directive is a directive that is used to bind the values of the HTML controls (input, select, and textarea) or any custom form controls, and stores the required user value in a variable and we can use that variable whenever we require that value. It also is used during form validations.
Ng-change is a directive in AngularJS which is meant for performing operations when a component value or event is changed. In other words, ng-change directive tells AngularJS what to do when the value of an HTML element changes. An ng-model directive is required by the ng-change directive.
Yes it is possible as long as you bind your input fields to custom event and then trigger that custom event on user confirmation.
http://plnkr.co/edit/GgsaFbIurhx88p6HJgkT?p=preview
<input bind-event type="text" name="userName" ng-model="user.name" ng-model-options="{ updateOn: 'customEvent'}" />
//bind-event directive for input fields.
.directive('bindEvent', function() {
return {
restrict: 'EAC',
controller: function($scope, $element, $attrs) {
$element.on('customEvent', function() {
console.log('custom event is triggered');
});
}
};
});
//trigger the event on submit.
$scope.click = function() {
$element.find( "input" ).triggerHandler( "customEvent" );
}
I found a simple way to do this today using $emit. I was trying to apply filter on a button click, rather than instantly(default behavior):
<input ng-model="myFilter.minPrice" ng-model-options="{updateOn: 'filterBtnClicked'}">
...
<button type="button" ng-click="$emit('filterBtnClicked')">Filter</button>
Model value is applied only after button is clicked.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With