javascript - 当 AngularJS 中的模型更新时, View 不会更新

标签 javascript html model-view-controller angularjs

我已阅读有关此问题的主题,例如:The view is not updated in AngularJS但我仍然不明白如何将它应用到我的简单示例中。

我有这个功能:

function MyPageView($scope) {
  var myModel = new MyModel();
  $scope.myModel = myModel;
}

myModel 在代码的其他地方更新时(当用户点击、交互、发送 XHR 请求时)它不会更新我的 View 。我知道我需要用 $apply 做点什么,但我不知道在哪里以及如何做。

有人可以向我解释如何解决这个简单用例的问题吗?

我的模型看起来像这样(如果这个问题是必要的)——它里面没有 AngularJS 代码:

var MyModel = function() {
  var _this = this;
  ...
  _this.load = function(){...};
  _this.updateModel = function(){...};
  ...
  return _this;
}

添加一个 JSfiddle 示例: http://jsfiddle.net/DAk8r/2/

最佳答案

更新模型后在 Controller 中尝试 $scope.$apply()

关于javascript - 当 AngularJS 中的模型更新时, View 不会更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14374440/

相关文章:

php - apache mod_security 说 - 访问被拒绝错误

javascript - 在 OneDrive 等网站应用程序中创建一个选择工具

javascript - 当按下 Enter 键时,文本区域不会停止换行

javascript - Vue 属性未定义

javascript - JQuery 嵌套了这个引用

html - 如何删除按钮样式或覆盖动态添加的类到 material2 中的按钮

C# MVC 在 OR 条件下使用多个属性

model-view-controller - 在管理层中从 MVC 路由生成 urls...

asp.net-mvc - 在 MVC Controller 中引用存储库

JavaScript 字符串拆分方法分隔符