问题描述
在最新的稳定版 Chrome 中运行最新的稳定版 Angular.我在带有 ng 控制器 A 的视图内的 DOM 元素上进行了 ng-click,但 DOM 元素本身有一个 ng-controller 设置为 another 控制器,B.ng-click 在控制器 B 内触发,但是 DOM 元素通过其 ng-model 属性绑定到的范围属性的更改不会反映出来.
Running latest stable version of Angular in latest stable Chrome. I have an ng-click on a DOM element that's inside a view with ng controller A, but the DOM element itself has an ng-controller set to another controller, B. ng-click fires inside controller B, however changes to scope properties, which DOM element is bound to by its ng-model attribute, are not reflected.
特别是我有一个 textarea 以及其他 HTML5 输入类型.没有错误,当我中断调试器时,我可以查看范围变量并看到 $scope 实际上指向控制器 B.
Specifically I have a textarea, as well as other HTML5 input types. No errors, and when I break in the debugger, I can look at the scope variable and see that $scope is in fact pointing to controller B.
Plnkr 示例在这里,注意它如何只更新同一 DIV 内的输入?:http://plnkr.co/edit/DmccNj?p=preview
Plnkr example here, notice how it only updates the input that's inside the same DIV?: http://plnkr.co/edit/DmccNj?p=preview
推荐答案
我最近遇到了同样的问题.显然,当您使用相同的 ng-controller
创建两个 HTML 片段时,它会创建控制器的两个单独实例.因此,点击编辑"按钮会在实例 1 中设置 $scope.comment
,但输入字段绑定到控制器的实例 2.
I had the same issue just recently. Apparently, when you create two HTML fragments with the same ng-controller
, it creates two separate instances of the controller. So, hitting the 'EDIT' button sets $scope.comment
in instance 1, but the input field is bound to instance 2 of the controller.
如果必须这样做,最好创建一个 Angular 服务,因为它们是单例的.
If you must do it this way, you're best of creating an angular service, as they are singletons.
这是我的问题:拆分 HTML一个控制器的组件导致控制器停止工作
此外,Angular Batarang 是一款非常酷的 Chrome来自 AngularJS 团队的扩展,用于调试 Angular 应用程序.我提到这一点是因为如果你(或我)使用了它,我们会立即看到问题所在.
Also, Angular Batarang is a very cool Chrome extension from the AngularJS team to debug Angular applications. I mention this because if you (or I) had used this, we would have instantly seen what was going wrong.
这篇关于模型更改时不会更新角度视图(DOM 元素)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!