【问题标题】:view is not getting refreshed with updated model视图未使用更新的模型刷新
【发布时间】:2015-02-23 09:49:03
【问题描述】:

模型更改时,我的角度视图没有更新。模型在指令中的“onblur”事件中发生变化。

inputNgEl.bind('blur', function () { })

我的模型正在传递给指令,指令正在显示模型的内容。

请在下面找到 plunk。 http://plnkr.co/edit/Ku5yOyWsa1fjnLIs2Eu3?p=preview

你能告诉我这里缺少什么吗?谢谢。

【问题讨论】:

  • 你的作用域变量只充当模型和视图之间的粘合剂......所以在作用域中定义你的变量(在模型中)

标签: angularjs validation angularjs-directive angular-directive angularjs-digest


【解决方案1】:

Angular 不知道您的模型更改:您需要使用 scope.$apply() 触发摘要

// only apply the has-error class after the user leaves the text box
inputNgEl.bind('blur', function () {
    //el.toggleClass('has-error', formCtrl[inputName].$invalid);
    //helpText.toggleClass('hide', formCtrl[inputName].$valid);
    scope.$apply(ValidateControl);

    // Or
    //scope.$apply(function () {
    //    ValidateControl();
    //});
});

【讨论】:

    【解决方案2】:

    以下变化对我有用。

    inputNgEl.bind('blur', function () { $timeout(function () { scope.$apply(ValidateControl); }, 0, false); });

    更正了http://plnkr.co/edit/kSnZbgY5AiaSfQxjbQke?p=preview

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-16
      • 2016-01-08
      • 2015-12-28
      相关资源
      最近更新 更多