亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

AngularJS,如何從API獲取數據并顯示

AngularJS,如何從API獲取數據并顯示

溫溫醬 2023-09-18 15:18:24
我試圖學習角度JS。我正在嘗試從以 JSON 格式提供藝術家數據的 API 中獲取數據。我正在嘗試做的是獲取在文本欄中輸入的名稱,然后僅將收到的信息顯示為 JSON。 API 接受的格式是 url/藝術家名稱 + 應用 ID 作為請求,其中藝術家名稱是輸入。我嘗試使用的 API 的人給了我一個有效的應用程序代碼,該代碼存儲在 var id 中。 到目前為止,我已經編寫了以下代碼,任何幫助將不勝感激。謝謝!    var myApp = angular.module('myApp', []);    myApp.controller('UserCtrl', function($scope,$http){        $scope.user = null;        var id = "my secret id comes here";        var name = $scope.searchText;        $scope.getByID = function() {            $http.get("https://rest.bandsintown.com/artists/" + name + id)                    $scope.user = response;                    console.log(response)                }              });接下來,html 代碼是:<body ng-app="myApp">    <div ng-controller="UserCtrl">        Search : <input type="text" placeholder="Search Employees" ng-model="searchText"/> <br/><br/>        <button ng-click="UserCtrl.getByID()">Submit</button></body>謝謝,對不起,如果這是一個新手問題,我確實是一個新手!
查看完整描述

1 回答

?
慕少森

TA貢獻2019條經驗 獲得超9個贊

response沒有在任何地方聲明,所以看起來你只需要正確處理響應。以下方法應該有效:$http.get


 var myApp = angular.module('myApp', []);

 myApp.controller('UserCtrl', function($scope, $http) {

   $scope.user = null;


   var id = "my secret id comes here";

   var name = $scope.searchText;


   $scope.getByID = function() {

     // $http.get returns a promise, you'll need to set the variables there.

     $http.get("https://rest.bandsintown.com/artists/" + name + id)

       .then(response => {

         $scope.user = response;

         console.log(response)

       })

   }

 });


查看完整回答
反對 回復 2023-09-18
  • 1 回答
  • 0 關注
  • 90 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號