Dejan.S Dejan.S - 6 months ago 19
AngularJS Question

store scope value, for it to be compared

I'm doing a

ng-blur
and sending the current value of a
input
into a
function
. My problem is that I want to save the previous input value and pass that into the blur function, because if value is same no action.

<input type="text" class="input__text" name="name" ng-model="name" ng-blur="bluryLines(name)">

$scope.bluryLines = function(oldValue, value) {
if (value !== '' | value !== oldValue) {
console.log('some action');
} else {
console.log('is empty');
}
};

Answer

Use ng-change to call your function and use ng-model-options="{ updateOn: 'blur' }"
Docs: https://docs.angularjs.org/api/ng/directive/ngModelOptions

<input type="text" class="input__text" name="name" ng-model="name" ng-change="bluryLines()" ng-model-options="{ updateOn: 'blur' }">

Plunker: https://plnkr.co/edit/BLFj5iRtF3xUxIQEk8UT?p=preview