【问题标题】:AngularJS does not return forward API query dataAngularJS 不返回转发 API 查询数据
【发布时间】:2022-01-25 22:55:46
【问题描述】:

我有一个小问题。下面代码中的 API 数据正常显示在控制台上,它返回一个包含数据的数组,但我无法返回它们以在前端使用它们。 PS:那个“名字在这里!”前面可以用,查询不行。

我正在使用 angularJS。

$scope.getCharacters = function() {
      $scope.teste = "name here!";
      axios.get(baseUrl + "/characters" + complemento)
        .then(function (res) {
          console.log(res.data.data.results) // returns API data normally, communication is working
          var heroes = res.data.data.results;
          return heroes;
        })
        .catch(function (err) {
          console.log(err)
        });
        
        console.log(heroes)
    }

$scope.getCharacters();

【问题讨论】:

  • 我不明白你的问题是什么。您是否检查过您的开发工具的“网络”选项卡以确认 HTTP 调用成功并且拥有您期望的数据?

标签: html angularjs angularjs-scope


【解决方案1】:

您需要将$scope.getCharacters(); 设置为某个变量。您正在调用代码,但它没有保存在任何地方以供使用。

$scope.heroes = $scope.getCharacters(); 是一个可能的解决方案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-19
    • 2014-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多