我正在为 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
,它应该会按预期工作。
关于javascript - $scope.$watch 不在 ui.bootstrap.modal Controller 中引发事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22334748/