简体   繁体   English

如何在使用带有angularjs的highcharts-ng执行setInterval函数时更改div中的文本

[英]How to change text in div while executing setInterval function using highcharts-ng with angularjs

I have a controller which has the following call : 我有一个controller ,它有以下调用:

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

here, userFactory is a factory. 这里, userFactory是一个工厂。

Now in my function renderCharts : 现在在我的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"};
    }

}

Following assignment doesn't update the value in ng-bind in the html view : $scope.current_target_label = {"label":"Target Score"}; 以下赋值不会更新html视图中ng-bind中的值: $scope.current_target_label = {"label":"Target Score"};

I am surely doing something wrong, appreciate for any pointers so as to what to do to get the value of text of div updated in the html view ? 我肯定做错了什么,欣赏任何指针,以便做什么来获得在html视图中更新div的文本的价值?

setInterval is outside of angulars $digest-cycle , so angular will not update data-bindings. setInterval angulars $ digest-cycle之外 ,因此angular不会更新数据绑定。 You could use $apply but, I'd say, that's bad practice. 你可以使用$apply但是,我会说,这是不好的做法。 Better use $timeout or $interval . 更好地使用$timeout$interval Wich will trigger the $digest-cycle . 这将触发$ digest-cycle Eg: 例如:

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

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

Notes: 笔记:

  • You'll, of course, need to inject the service where you need it. 当然,您需要在需要的地方注入服务。
  • In your original code you used setInterval(function($scope) { . $scope here will probably be undefined, and thus shadow the outer $scope variable. 在原始代码中,您使用了setInterval(function($scope) {$scope这里可能是未定义的,因此会影响外部$scope变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM