我試圖學習角度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)
})
}
});
- 1 回答
- 0 關注
- 90 瀏覽
添加回答
舉報
0/150
提交
取消