$scope 变量未传递给请求 header / 未定义的值

$scope variable not passed to the request header / undefined value

我试图让下面代码中的最后一个调用正常工作,但 $scope.token 变量有问题。它以未定义的值传递给服务。我试图一次将一个变量传递给 getAccessToken 函数,但它也不起作用。 有趣的是,$scope.client 和 $scope.tenant 变量都正确传递了。

我找到了很多关于 .service 或 .factory 的资源,它们还可以让我将此调用分离到另一个控制器中,但对于初学者来说,这些主题太高级了。

我完全不熟悉编程,所以我可能犯了一些愚蠢的错误。

var defaultListApp = angular.module('defaultListApp', ['build', 'build_editors']);
defaultListApp.controller('authorizationCtrl', ['$scope', '$location', 'Restangular', '$http', function($scope, $location, Restangular, $http){

  Restangular.setBaseUrl('https://api.xxx.io/xxx/xxx/');
  Restangular.one('projects/' + Build.currentProjectId + '/clients').getList().then(function(items){
  $scope.items = Restangular.stripRestangular(items);
  console.log("ITEMS: " + $scope.items);
},function(error){
  //handle error
});

$scope.selectClient = function(client) {
$scope.client = client.id;
Restangular.setBaseUrl('https://api.xxx.io/xxx/xxx/');
Restangular.one('projects/' + Build.currentProjectId + '/clients/' + $scope.client + '/credentials').get().then(
  function(response){
    $scope.credentials = Restangular.stripRestangular(response);
    $scope.clientId = $scope.credentials[0].clientId;
    $scope.clientSecret = $scope.credentials[0].clientSecret;
    $scope.getAccessToken($scope.client, $scope.clientSecret);
    $scope.tenant = Build.currentProjectId;
    $scope.getAppRepresentation($scope.client, $scope.tenant, $scope.token);
});
};

$scope.getAccessToken = function(oauthPayload){
$http({
                method: 'POST',
                url:  'https://api.xxx.io/xxx/oauth2/token',
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                data: 'grant_type=client_credentials' +
                '&scope=xxx.view xxx.manage' +
                '&client_id=' + $scope.clientId +
                '&client_secret=' + $scope.clientSecret
            }).then(function(response) {
                $scope.token = response.data.access_token;
                $scope.getAppRepresentation($scope.client, $scope.tenant, $scope.token);
                var path = "/calls.html";
                window.location.href = path;


            })
          };

$scope.getAppRepresentation = function(getApp){
$http({
                method: 'GET',
                url:  'https://api.xxx.io/xxx/documents/' + $scope.tenant + '/' + $scope.client,
                headers: {'Authorization': 'Bearer ' + $scope.token }
                  }).then(function(response) {
                $scope.appData = response.data;
                console.log($scope.appData);
            })
          }

在尝试使用 $scope.getAppRepresentation()

中的令牌之前,您没有等待 $scope.getAccessToken() 完成

可以这样做:

$scope.getAccessToken = function(oauthPayload){
    // return promise 
    return $http({.......})
}

然后更改调用顺序:

$scope.getAccessToken($scope.client, $scope.clientSecret);    
$scope.getAppRepresentation($scope.client, $scope.tenant, $scope.token);

$scope.getAccessToken($scope.client, $scope.clientSecret).then(function(){
     $scope.getAppRepresentation($scope.client, $scope.tenant, $scope.token);
});

注意:如果您仅在控制器内部使用它们并且在视图中不需要它们,则没有理由将每个函数都定义为 $scope 的一部分。其实有点混乱