最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular view (DOM elements) not being updated when model changes - Stack Overflow

programmeradmin2浏览0评论

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.

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 example here, notice how it only updates the input that's inside the same DIV?:

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.

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 example here, notice how it only updates the input that's inside the same DIV?: http://plnkr.co/edit/DmccNj?p=preview

Share Improve this question edited Apr 3, 2013 at 0:16 brushleaf asked Apr 2, 2013 at 23:40 brushleafbrushleaf 1,2154 gold badges18 silver badges26 bronze badges 4
  • 2 Can you post a Plunker or at least some code? – Josh David Miller Commented Apr 2, 2013 at 23:43
  • Josh, working on one now. – brushleaf Commented Apr 2, 2013 at 23:54
  • most likely a scope inheritance issue due to using a primitive in scope.... read this and try the dot remendation github./angular/angular.js/wiki/… – charlietfl Commented Apr 3, 2013 at 0:02
  • Notice how in the Plunker it only updates one of the two inputs, not the one in a different div, even though they have the same controller. – brushleaf Commented Apr 3, 2013 at 0:17
Add a ment  | 

1 Answer 1

Reset to default 7

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.ment 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.

Here is my question: Splitting HTML ponents for one controller causes controller to stop working

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.

发布评论

评论列表(0)

  1. 暂无评论