3

我有一个controller具有以下调用的:

userFactory.getScore(_token, function (response) {
    $scope.scores = response;
    renderCharts(response,$scope);
    score.resolve();
});

这里,userFactory是一家工厂。

现在在我的function renderCharts

function renderCharts(response,$scope) {

    $scope.current_target_label = {"label":"Current Score"};
    // Highcharts config object declaration here

    setInterval(function($scope) {
        // Logic to update highchart
        $scope.current_target_label = {"label":"Target Score"};
    }

}

以下分配不会更新html视图中 ng-bind 中的值:$scope.current_target_label = {"label":"Target Score"};

我肯定做错了什么,感谢任何指针,以便如何获取视图中div更新文本的值html

4

1 回答 1

3

setInterval在angulars $digest-cycle之外,因此 angular 不会更新数据绑定。你可以使用,但我想说,这是不好的做法。更好地使用或. Wich 将触发$digest-cycle。例如:$apply$timeout$interval

function renderCharts(response,$scope) {
    // ...

    $interval(function() {
        // Logic to update highchart
        $scope.current_target_label = {"label":"Target Score"};
    }, delay);
}

笔记:

  • 当然,您需要在需要的地方注入服务。
  • 在您使用的原始代码中setInterval(function($scope) {$scope这里可能是未定义的,因此会影响外部$scope变量。
于 2015-06-30T07:15:27.383 回答