我正在尝试从此服务获取天气信息,并使用 AngularJS 库将动态数据填充到 html 中。
http://api.openweathermap.org/data/2.5/weather?q=Riyadh,ksa&appid=a809c777d6813b6b0905a9a7bf1a8399
我应该把这个链接放在脚本标签中吗?如果是,我如何将它与 AngularJS 一起使用。这是我第一次使用 api 和 AngularJS,我看了很多教程,但没有一个像这个 api 链接。
最佳答案
你应该这样做
app.controller("testController", function($scope,testService){
testService.getData ()
.then(function (response) {
$scope.testData = response.data;
// handle valid reponse
},
function(error) {
//handel error
});
}
始终处理错误。
你还应该创建一个服务
angular.module('myApp')
.service('testService', function ($http) {
this.getData = function () {
var endpoint = "url/";
return $http({
method: 'get',
url: endpoint
});
};
}
有关详细 View ,请查看 this简单的应用
关于html - 使用 Angular JS 激活 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45671324/