簡體   English   中英

$ http計時問題,AngularJS

[英]$http timing issues, AngularJS

有兩個問題。 我試圖從$ http響應中獲取一個值,並用它填充一個變量,然后應該更新幾個DOM對象。 問題是它似乎有一個計時問題,其中調用$ http服務的函數完成,然后變量得到更新,但似乎沒有更新它應該更新。 我也嘗試對變量進行監視,它似乎只是在最初加載頁面時觸發。 我整天都在讀這一切,似乎無法找到有效的答案。

app.controller('MainCtrl', ['$scope', '$http', 'waciServ', function($scope,      $http, waciServ) {
"use strict";
$scope.currentSource = waciServ.activeSource;

$scope.$watch('waciServ.activeSource', function(newValue, oldValue) {
        $scope.currentSource = newValue;
        console.log('Watcher! ' + newValue);
}/*, true*/);

$scope.getActiveSource = function () {
    $scope.currentSource = waciServ.getStringByName("active_device");
};
}]);

app.service('waciServ', function($http) {
  var self = this;
  this.waciIP = location.host;
  this.activeSource = '';

  this.getStringByName = function (name) {
    $http.post("http://" + self.waciIP + "/rpc/", "method=GetVariableByName&param1=" + name + "&encoding=2")
        .then (function (response) {
            var was_error = self.read(response.data);

            if (was_error == '1') { //active_device is not set
                self.assignVariable(name, "none");
                self.activeSource = "none";
                return self.activeSource;

            } else {
                var varId = parseInt(self.read(response.data));
                $http.post("http://" + self.waciIP + "/rpc/", "method=GetVariableValue&param1=" + varId + "&encoding=2")
                    .then (function (response) {

                        self.activeSource = self.read(response.data);

                        return self.activeSource;      
                });
            }
    }, function (error) {
        console.log("error: " + error.data);
    });
  };
});

我可以在返回觸發之前放置一個console.log並看到我有我想要的東西,但是在控制器內的函數中放置的另一個console.log顯示為'undefined'。

是什么賦予了? 先感謝您。

你不需要考慮使用觀察者。

基本上問題是你沒有從服務方法返回承諾。 您應該從服務方法返回$http方法調用的承諾。然后使用.then over方法調用鏈承諾並在其中放置successerror函數。 這個答案與你提出的問題很相似,但並不完全相同)

服務

self.getStringByName = function(name) {
  //returned promise from here
  return $http.post("http://" + self.waciIP + "/rpc/", "method=GetVariableByName&param1=" + name + "&encoding=2")
    .then(function(response) {
    var was_error = self.read(response.data);

    if (was_error == '1') { //active_device is not set
      self.assignVariable(name, "none");
      self.activeSource = "none";
      return self.activeSource; //returned data here to chain promise
    } else {
      var varId = parseInt(self.read(response.data));
      //returned promise from here
      return $http.post("http://" + self.waciIP + "/rpc/", "method=GetVariableValue&param1=" + varId + "&encoding=2")
        .then(function(response) {
        self.activeSource = self.read(response.data);
        //returned data from here
        return self.activeSource;
      });
    }
  }, function(error) {
    console.log("error: " + error.data);
  });
};

調節器

app.controller('MainCtrl', ['$scope', '$http', 'waciServ', function($scope,      $http, waciServ) {
"use strict";
   $scope.currentSource = waciServ.activeSource;

   $scope.getActiveSource = function () {
      waciServ.getStringByName("active_device").then(function(source){
         $scope.currentSource = source;
      });
   };
}]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM