javascript - $scope.$watch 不在 ui.bootstrap.modal Controller 中引发事件

标签 javascript angularjs angular-ui-bootstrap

我正在为 Angular 使用 UI Bootstrap ,并且已将 ui.bootstrap.modal 组件集成到我的解决方案中。除了一件事,一切都工作正常。我已经设置了一个 $scope.$watch 来监视位于模态后面的 Controller 上特定属性的更改,但是,当属性更改时它不会触发。

我已经在 plunkr 中转载了它

如您所见,它会在模式打开时触发一次,但不会在您更改 UI 中的复选框值时触发。乍一看,绑定(bind)似乎很好,因为更新一个复选框会更新另一个复选框(两者都绑定(bind)到相同的属性)。

有什么想法吗?

谢谢!

最佳答案

Angular $watch statements总是至少触发一次以设置绑定(bind)

After a watcher is registered with the scope, the listener fn is called asynchronously (via $evalAsync) to initialize the watcher. In rare cases, this is undesirable because the listener is called when the result of watchExpression didn't change. To detect this scenario within the listener fn, you can compare the newVal and oldVal. If these two values are identical (===) then the listener was called due to initialization.

您收到有关设置的警报,但它似乎没有绑定(bind)任何东西,这就是为什么您没有看到它再次被调用的原因。

您看到的是由于原型(prototype)继承和基元导致的 Angular 限制。如果您将其更改为 $scope.form = {}; 并将其引用为 form.chkVariable,它应该会按预期工作。

http://plnkr.co/edit/3hCJ3EM1Gx1fCDSePQfa?p=preview

关于javascript - $scope.$watch 不在 ui.bootstrap.modal Controller 中引发事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22334748/

相关文章:

javascript - 有更好的 onchange 吗?

javascript - 启用/禁用温泉按钮

javascript - Angular 无法使用模板渲染轮播

angularjs - 如何在 Angular UI Bootstrap 中查看模态是否打开

javascript - 在模态窗口 AngularJS 中动画显示/隐藏 Ui Bootstrap 警报

javascript - 如何使用多个对象映射 API [Spree API V2 & ReactJS]

javascript - JavaScript 警告框中的新行

javascript - Angular 2模拟Http get()返回本地json文件

javascript - 将 ng-options 与嵌套数组一起使用

javascript - 使用诱饵会破坏我在 IE 上的上传。为什么?