javascript - Angularjs - 我得到了 data.data,但无法为其分配范围变量

标签 javascript angularjs amazon-web-services http

我正在开发一个基于 AngularJS 的网站,该网站从 Amazon Web Services 表中获取问题和多项选择答案,以便动态构建实验前问题页面。 http get 工作正常 - 当我放置断点并评估 data.data 的值时,它提供了我正在寻找的所有数据。

但是,在将其分配给 $scope.allQuestions 后,该变量在控制台中的计算结果为 undefined$scope.allAnswers 也是如此。在不同的页面上,我使用相同的代码毫无问题地获取缩略图/模拟 URL。

$location 工作正常,我注入(inject) $filter 以便稍后在 ng-repeat 中进行过滤。

我使用 AWS 上的 API 网关来调用它。我启用了 CORS,并且我对此处的两个函数使用了与我在其他有效函数中使用的相同设置。

代码如下。提前致谢!

app.service('preLabService', function($http) {
  this.getQuestions = function() {
    return $http.get('api-gateway-url');
  };
  this.getAnswers = function() {
    return $http.get('api-gateway-url');
  };
});

app.controller('PreLabQuestionsCtrl', ['$scope', '$location', '$filter', 'preLabService', function($scope, $location, $filter, preLabService) {
  init();

  function init() {
    preLabService.getQuestions().then(function(data) {
      $scope.allQuestions = data.data;
    });
    preLabService.getAnswers().then(function(data) {
      $scope.allAnswers = data.data;
    });
    $scope.simURL = $location.search().simURL;
    $scope.simID = $location.search().SimID;
  }
}]);

在我实际在浏览器中运行的代码中,我将 *'api-gateway-url' 替换为它们各自的 url - 我只是不想在这里给出 API url。

最佳答案

您真的需要将 thi 添加到 $scope 变量吗?

你能试试吗:

const vm = this

然后将数据值赋给vm.allQuestions和vm.allAnswers?

关于javascript - Angularjs - 我得到了 data.data,但无法为其分配范围变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45737762/

相关文章:

php - 需要在 JavaScript 中循环遍历 PHP 数组

amazon-web-services - Windows Server 2016 ADFS - 与 AWS Directory Service 集成

amazon-web-services - AWS 上的 Kubernetes 1.4 SSL 终止

javascript - jQuery div 从左到右动画并在暂停后返回

javascript - 如何测试 flexbox div 是否设置为 `display:block` ?

javascript - 在 ngTables 中,第三次运行 $scope.tableParams.reload() 导致 TypeError : Cannot set property '$data' of null

javascript - 无法添加 "$localStorage"

javascript - 如何检查 Angular 是否已经有模块?

amazon-web-services - 当SNS调用Lambda时,是否总是只有1条记录?

javascript - 从缩进树中的闭合节点开始