AngularJS-设置视图值未更新显示 [英] Angularjs - set view value is not updating display

查看:92
本文介绍了AngularJS-设置视图值未更新显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了一个输入指令,该指令应允许用户撤消操作.

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

Enter 使用某些功能保存该值, Esc 取消上一次保存的编辑.

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

对于 Esc 按键事件,我正在使用ngmodel.$setViewValue(scope.last_saved_value),但是输入未更新.我从文档知道此函数不会触发$digest我将其放在$apply中,但仍然无法正常工作.

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示例

推荐答案

我通常都包含并要求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;
});

这篇关于AngularJS-设置视图值未更新显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆