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

查看:31
本文介绍了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)输入未更新.我从 docs 知道这个函数不会触发 $摘要 所以我把它放在 $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:

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;
});

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

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