Angularjs - set view value does not refresh display

I have an input directive that should allow users to cancel.

Enter saves the value using some function, Esc Undo changes from the last save.

For the Esc keypress event, I use ngmodel.$setViewValue(scope.last_saved_value) , but the input is not updated . I know from docs that this function does not run $digest , so I put it in $apply , but it still does not work.

JSBIN example

+6
source share
2 answers

I usually include and require ngModel :

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

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

 scope.$apply(function() { scope.ngModel = scope.last_saved_value; }); 
+2
source

Try calling ngmodel.$render(); after doing $setViewValue , this should help.

+35
source

All Articles