AngularJS无法访问函数外部的变量

时间:2018-10-26 09:12:29

标签: javascript angularjs

我无法访问'apiURL',我在控制台日志中收到此错误

  

angular.min.js:113 GET http://127.0.0.1:3000/apiURL 404(未找到)

JS:

$scope.searchFunction = function() {
  for (var i = 0; i < $scope.allTheme.length; i++) {
    if ($scope.selected == $scope.allTheme[i].THEMENAME) {
      $scope.selectedTheme = $scope.allTheme[i].QUERYNAME;
      apiURL = 'https://developers.onemap.sg/publicapi/themeapi/retrieveTheme?queryName=' + $scope.selectedTheme +
        '&token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjMsInVzZXJfaWQiOjMsImVtYWlsIjoicHVibGljQXBpUm9sZUBzbGEuZ292LnNnIiwiZm9yZXZlciI6ZmFsc2UsImlzcyI6Imh0dHA6XC9cL29tMi5kZmUub25lbWFwLnNnXC9hcGlcL3YyXC91c2VyXC9zZXNzaW9uIiwiaWF0IjoxNTQwNDU0NTQ2LCJleHAiOjE1NDA4ODY1NDYsIm5iZiI6MTU0MDQ1NDU0NiwianRpIjoiOWNlOGU3MGZmZTc3MTk5NjY1YjE0Mzc5MzZkN2IzNTIifQ.Is-oNPt8J7qfMkl0RbVDse84tD5rRhwG7d7LGr9i6xQ';
    }
  }
}

$http.get('apiURL').then(function(response) {
  $scope.apiResult = response.data.SrchResults;
  $scope.apiResult.splice(0, 1);
})

1 个答案:

答案 0 :(得分:-1)

您通过apiURL作为字符串$http.get('apiURL')

$scope.searchFunction = function() {
  for (var i = 0; i < $scope.allTheme.length; i++) {
    if ($scope.selected == $scope.allTheme[i].THEMENAME) {
      $scope.selectedTheme = $scope.allTheme[i].QUERYNAME;
      let apiURL = 'https://developers.onemap.sg/publicapi/themeapi/retrieveTheme?queryName=' + $scope.selectedTheme +
        '&token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjMsInVzZXJfaWQiOjMsImVtYWlsIjoicHVibGljQXBpUm9sZUBzbGEuZ292LnNnIiwiZm9yZXZlciI6ZmFsc2UsImlzcyI6Imh0dHA6XC9cL29tMi5kZmUub25lbWFwLnNnXC9hcGlcL3YyXC91c2VyXC9zZXNzaW9uIiwiaWF0IjoxNTQwNDU0NTQ2LCJleHAiOjE1NDA4ODY1NDYsIm5iZiI6MTU0MDQ1NDU0NiwianRpIjoiOWNlOGU3MGZmZTc3MTk5NjY1YjE0Mzc5MzZkN2IzNTIifQ.Is-oNPt8J7qfMkl0RbVDse84tD5rRhwG7d7LGr9i6xQ';
    }
  }
}

$http.get(apiURL).then(function(response) {
  $scope.apiResult = response.data.SrchResults;
  $scope.apiResult.splice(0, 1);
})