您是正确的,以下手表导致了问题:
// Watching on data changing
scope.$watch('data', function (newData, oldData) {
if (newData !== oldData && scope.chart) {
if (!scope._config.disabled && scope._config.autorefresh) {
scope._config.refreshDataOnly && scope.chart.update ? scope.chart.update() : scope.api.refresh(); // if wanted to refresh data only, use chart.update method, otherwise use full refresh.
}
}
}, scope._config.deepWatchData);
问题是,如果你在这个函数中添加一个日志来查看新旧数据是什么,数据实际上是在变化的。
当您单击其中一个点时,nvd3 正在向数据添加颜色属性。
旧数据:
[{"key":"Battery Voltage","values":[{"x":1439419440000,"y":90,"series":0},{"x":1439419440000,"y":43,"series":0},{"x":1439419440000,"y":345,"series":0},{"x":1439167620000,"y":73,"series":0},{"x":1439167620000,"y":42,"series":0},{"x":1439167620000,"y":36,"series":0},{"x":1440010740000,"y":32,"series":0},{"x":1439419440000,"y":62,"series":0},{"x":1439167620000,"y":73,"series":0},{"x":1439167620000,"y":42,"series":0},{"x":1439167620000,"y":36,"series":0}]}]
新数据:
[{"key":"Battery Voltage","values":[{"x":1439419440000,"y":90,"series":0,"color":"#1f77b4"},{"x":1439419440000,"y":43,"series":0,"color":"#1f77b4"},{"x":1439419440000,"y":345,"series":0},{"x":1439167620000,"y":73,"series":0},{"x":1439167620000,"y":42,"series":0},{"x":1439167620000,"y":36,"series":0},{"x":1440010740000,"y":32,"series":0},{"x":1439419440000,"y":62,"series":0,"color":"#1f77b4"},{"x":1439167620000,"y":73,"series":0},{"x":1439167620000,"y":42,"series":0},{"x":1439167620000,"y":36,"series":0}],"color":"#1f77b4","value":90}]
每次点击一个新的点,都会给对象添加颜色属性,所以 watch 函数实际上是正确运行的。
我建议在https://github.com/krispo/angular-nvd3上打开一个问题
**编辑 - 澄清一下,添加 ng-click 时发生这种情况的原因是因为这会强制 Angular 摘要循环运行,这将触发监视。