角视图(DOM元素)不被当模型更改更新不被、视图、模型、元素

2023-09-13 05:13:39 作者:玖天揽月

在最新的稳定运行Chrome浏览器的角最新的稳定版本。我有与NG控制器A中的视图内的DOM元素上的NG-点击,但DOM元素本身具有的NG-控制器设置为的其他的控制器,B. NG单击控制器B内火灾然而变为作用域属性,这DOM元素绑定到其NG-模型属性,都没有体现出来。

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输入类型。没有错误,当我在调试器打破,我可以看看范围的变量,并看到$范围实际上是在指向控制器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=$p$pview

Plnkr example here, notice how it only updates the input that's inside the same DIV?: http://plnkr.co/edit/DmccNj?p=preview

推荐答案

我有同样的问题,刚刚结束。显然,当你创建两个HTML片段具有相同 NG-控制器,它会创建控制器的两个单独的实例。因此,击中编辑按钮可以将 $ scope.comment 实例1,但输入字段绑定到控制器的实例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.

如果你必须这样做,你最好创建一个角度的服务,因为他们是单身。

If you must do it this way, you're best of creating an angular service, as they are singletons.

下面是我的问题:Splitting一个控制器HTML组件会导致控制器停止工作

此外,Angular Batarang 是从AngularJS队一个非常酷的Chrome扩展来调试应用角度。我提到这一点,因为如果你(或我)这样做,我们会立刻看到发生了什么事情错了。

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.