Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从控制器重复调用angularjs服务以更新视图_Angularjs_Angularjs Directive_Angularjs Scope - Fatal编程技术网

如何从控制器重复调用angularjs服务以更新视图

如何从控制器重复调用angularjs服务以更新视图,angularjs,angularjs-directive,angularjs-scope,Angularjs,Angularjs Directive,Angularjs Scope,我正在尝试根据服务器响应每秒钟更新一次视图。我正在使用实时数据显示应用程序。我需要每秒钟向服务器发送一次请求,并在我的视图中获得响应和显示。目前我正在使用jquery和ajax进行请求和响应 刚才我正在尝试使用angularjs,并使用下面的代码从服务器获取响应 'use strict'; angular.module('liverate', ['dataServices']); angular.module('dataServices', []). service('Data', ['$h

我正在尝试根据服务器响应每秒钟更新一次视图。我正在使用实时数据显示应用程序。我需要每秒钟向服务器发送一次请求,并在我的视图中获得响应和显示。目前我正在使用jquery和ajax进行请求和响应

刚才我正在尝试使用angularjs,并使用下面的代码从服务器获取响应

'use strict';

angular.module('liverate', ['dataServices']);

angular.module('dataServices', []).
service('Data', ['$http', function ($http) {
    var my_Date = new Date();
    var rates = {};
     var urlBase = BASE_URL+"api/apirate.php"+"?nocache=" + my_Date.getUTCSeconds();

     this.getRates = function () {
        $http({
              method: 'GET',
              url: urlBase,
              //headers: {'Content-Type': 'application/x-www-form-urlencoded'},
              success: function(data){
                   console.log(data);
                   rates = data;
              },
              error : function(statusCode,error) {
                  console.log(error);
              }
            });
        return rates;
     };
}]);

function LiverateController($scope, $timeout, Data) {
    $scope.data = [];
    var rates = {};
    (function tick() {
        $scope.data = Data.getRates(function(){
            $timeout(tick, 1000);
        });
    })();

    And also tried bellow method

    $scope.callAtTimeout = function() {
        console.log(Data.getRates());
    }

    $timeout( function(){ $scope.callAtTimeout(); }, 1000);

};

但这对我不起作用。我需要如何使用这个。请任何人帮帮我。

看来你不太明白
$http
/promissions是如何使用的。您需要返回
$http
返回到调用函数的承诺,或从该承诺派生的承诺,如以下代码:

angular.module('dataServices', []).
service('Data', ['$http', '$q', function ($http, $q) {
  var my_Date = new Date();
  var rates = {};
  var urlBase = BASE_URL+"api/apirate.php"+"?nocache=" + my_Date.getUTCSeconds();

  this.getRates = function () {
    return $http({
       method: 'GET',
       url: urlBase,
    }).then(function(results) {
       console.log(results);
       return results.data;
    }, function(error) {
       console.log(error);
       return $q.reject(error);
    });
  };
}]);

function LiverateController($scope, $timeout, Data) {
   $scope.data = [];
   var rates = {};
   (function tick() {
     Data.getRates().then(function(data){
       $scope.data = data;
     })['finally'](function() {
       $timeout(tick, 1000);
     });
   })();
};

  • 我倾向于在
    $interval
    上使用
    $timeout
    ,因为天真地使用
    $interval
    可能会在服务器速度有点慢的情况下影响服务器

  • $timeout
    的调用实际上是在
    finally
    回调中,在请求成功或失败的情况下运行


对于实时应用程序,应使用WebSocket。至少您应该在更新模型的服务中使用长轮询。控制器应使用模型,并允许角度绑定在模型更改时更新视图。控制器应该是哑的,不做任何轮询。非常感谢。我挣扎了很长时间。这对我帮助很大。像我期待的那样工作。