Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angularjs - set view value is not updating display

I got an input directive that should allow users to undo.

Enter saves the value using some function, Esc Cancel edits from the last save.

For the Esc keypress event i'm using ngmodel.$setViewValue(scope.last_saved_value) but the input is not updating. I know from the docs that this function does not trigger $digest so i put it in an $apply but it is still not working.

JSBIN example

like image 827
haki Avatar asked Apr 05 '14 07:04

haki


2 Answers

Try calling ngmodel.$render(); after you do $setViewValue, it should help.

like image 118
Ivan Koshelev Avatar answered Nov 08 '22 10:11

Ivan Koshelev


I usually both include and require the ngModel:

app.directive('cancelableInput', function($timeout) { 
  return { 
    restrict : "A",
    require : 'ngModel', 
    scope: {
      ngModel: '=?'
    },

Then, when you want to change the model value and have it update, you can just do:

scope.$apply(function() {
  scope.ngModel = scope.last_saved_value;
});
like image 36
dave Avatar answered Nov 08 '22 08:11

dave