Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.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
Javascript 角度js http get_Javascript_Angularjs_Get_Ionic Framework - Fatal编程技术网

Javascript 角度js http get

Javascript 角度js http get,javascript,angularjs,get,ionic-framework,Javascript,Angularjs,Get,Ionic Framework,我对angularJS不太熟悉,在尝试使用http.get()时遇到困难。我正在完成二维码扫描,从二维码接收的文本将被放入我的url中。我收到的问题是http.get()在扫描完成之前执行。因此返回“错误”。如何使http.get(url)仅在$scope.QRScan()函数完成后执行 $scope.QRscan(); /// Want to finish first var params = "?number=" + $scope.QRText; params += "&

我对angularJS不太熟悉,在尝试使用http.get()时遇到困难。我正在完成二维码扫描,从二维码接收的文本将被放入我的url中。我收到的问题是http.get()在扫描完成之前执行。因此返回“错误”。如何使http.get(url)仅在$scope.QRScan()函数完成后执行

  $scope.QRscan(); /// Want to finish first

  var params = "?number=" + $scope.QRText;
  params += "&action=ci";

  var url = "http://test/test.php" + params;

  var promise = $http.get(url);

  promise.then(
    function(payload) {
      var r = payload.data;

      if (r.status.toString() == '1') {
        var alertPopup = $ionicPopup.alert({
          title: ' successful ',
        });
      } else {
        var alertPopup = $ionicPopup.alert({
          title: 'Error',
        });

      };
    });
QRScan()

$http.get()是异步的

你可以这样写:

function getData() {
  return $http.get(url)
    .then(function(data) {
    // this is where we can manipulate your data
    // set to $scope object/whatever
    // because its async, we need to use a promise (or callback) to wait for
    // the response from your get request
  })
  .catch(function(err) {
    // if err, console.log(err)
  })
}

有几种方法可以做到这一点,上面是在以下文档中的“快捷方式方法”下:$http

this$scope.QRscan()是返回承诺还是接受回调函数?node.js是非阻塞的,这意味着不保证$scope.QRscan()将在var params=“?number=“+$scope.QRText;您必须使用callback或等待promise ResolvingTanks!这和文档帮助我解决了问题。
function getData() {
  return $http.get(url)
    .then(function(data) {
    // this is where we can manipulate your data
    // set to $scope object/whatever
    // because its async, we need to use a promise (or callback) to wait for
    // the response from your get request
  })
  .catch(function(err) {
    // if err, console.log(err)
  })
}