Angular ng-change in input textbox with old value
Asked Answered
E

4

12
<input type="text" id="exampleab"
ng-model="a.b"
ui-event="{ blur : 'callScriptThenServer()' }" >

For some reasons the ng-change on textbox is not working so i am using it; Using Angular-ui's ui-events.

PROBLEM

I want to call the function only if the value is changed and also want the old value in callback.(since I want to send the oldValue to the server).

I don't want to go via pure directives route because there are so many occurrences of these

NG-CHANGE : on each character changed i get a callback . I don't want that. I need to call the server script .. with the old value in the text box and the new value after blur

Escadrille answered 3/1, 2014 at 16:37 Comment(5)
ng-change works just fine. Check this minimal reproducible example plnkr.co/edit/UVG8rPhBa5vrwn6UR7hr?p=previewHeparin
you will need to type something in the box to use that example. on change it will alertHeparin
on tying each character i get a callback .. I DON't WANT THAT .. i need to call the server script .. with the old value in the text box and the new value after blurEscadrille
ng-change was not working since i was using old version of ngularjsEscadrille
okay great, now you have new angularjs just use ng-blur= instead of ng-change= so that when user has finished with the input the value will be sent to the server (note in some cases users will type a character and switch to next input for that you need to implement a chekc inside your controller)Heparin
M
17

You can watch your variable to have the newValue and oldValue at the same time.

<input type="text" id="exampleab" ng-model="a.b"  >

In your controler:

app.controller('ctrl', function ($scope) {
    $scope.$watch('a.b', function (newValue, oldValue) {
        console.log('oldValue=' + oldValue);
        console.log('newValue=' + newValue);
        //do something
    });
});

JSFiddle

EDIT You mentioned a new requirement in your edit so i edit my answer. You shouldn't use ng-change. you should get the oldValue when the control being focused and save it in a variable and then get the new value on blur event. I set up a new fiddle.

In your controller :

    app.controller('ctrl', function ($scope) {
        $scope.showValues = function () {
            alert('oldValue = ' + $scope.oldValue);
            alert('newValue = ' + $scope.a.b);
        }
    });

I your view

<input type="text" id="exampleab" ng-model="a.b" ng-init="oldValue = ''" ng-focus="oldValue = a.b" ng-blur="showValues()" />{{a.b}}
Madonna answered 3/1, 2014 at 16:45 Comment(5)
on tying each character i get a callback .. I DON't WANT THAT .. i need to call the server script .. with the old value in the text box and the new value after blurEscadrille
@naveen23: You need to work on your writing, which sounds really rude and unthankful.Abdias
@phresnel Why does he need to? When people does not read his requirements correctly... Its his waste of time...Herminiahermione
@Elisa: If a partial misunderstanding in the effort of helping some stranger for free justifies being rude and aggressive in your world, I am glad we are not friends. You don't even realise that it's especially the answerers waste of time if there's a misunderstanding, not the askers. -- edit: OTOH, your opinion does no longer surprise me after seeing your 1/10 answer/ask ratio, or more specifically 1/40 with actual upvotes. This time, seemingly an indicator of non-experience in donating some free time. Are you a parasite?Abdias
@Madonna Thanks a lot for such a great solution. I was playing with ng-change. Now I replaced it with ng-focus and ng-blur events. It works fine. I'm getting old and new values. Cheers!!!Accipitrine
C
10

Use ng-model-options

<input type="text" ng-model="a.b" ng-change="callScriptThenServer()" ng-model-options="{updateOn: 'blur'}"/>
Crapulent answered 16/5, 2014 at 15:24 Comment(0)
B
2

You can use AngularJS Watch

$scope.$watch(function(){
    return $scope.a.b;
}, function(newvalue, oldvalue){
    //Here You have both newvalue & oldvalue
    alert("newvalue: " + newvalue);
    alert("oldvalue: " + oldvalue);
},true);

Plunkr DEMO

Buhler answered 3/1, 2014 at 16:44 Comment(1)
on tying each character i get a callback .. I DON't WANT THAT .. i need to call the server script .. with the old value in the text box and the new value after blurEscadrille
M
0

Use Angular ngModelOptions with getterSetter:true; and then use method call inside the "set" part of the function.

See the last example on this page: https://docs.angularjs.org/api/ng/directive/ngModelOptions

Mcfarlin answered 20/2, 2015 at 10:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.