根据请求,我将简化我的问题..但因为它与本地主机上的 API 交互,所以我无法在 jsfiddle 上复制该问题。我将把我的问题分解为主 Controller 和子 Controller 之间发生的事情的细节......
主 Controller :范围包含公司 ID 并使用 get 函数从 API 访问数据。
子 Controller :继承从API返回的对象并将数据注入(inject)到highchart中。该 Controller 还包含一个用于遍历 highchart 中数据的 UI 的 $watchCollection。
我的问题是这样的:当父范围中的公司 ID 发生更改并且 API 获取新数据时,图表不愿意立即更新,因为 $watchCollection 不包含父范围中的内容。
我知道这个想法是将更改 $broadcast 到子 Controller 并进行相应更新,但图表仍然不会更新,直到 UI 上发生更改,从而触发 $watchCollection。
当父范围发生变化时,我该如何强制图表触发 $watchCollection?
最佳答案
子作用域原型(prototype)继承父作用域的属性,因此您应该能够直接 $watch
currentCoId
或数据。
另请参阅此 short demo 。
(请注意, $watchCollection()
仅检测在集合中添加/删除/移动项目,而不查看其内容。)
关于javascript - Highcharts 和 Angularjs : update chart from $broadcast in the main controller doesn't update the chart like a $watch would in the chart's controller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22900434/