This is my HTML Code.
<!DOCTYPE html>
<html ng-app>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="css/style.css">
<script src="js/angular.js"></script>
<script src="js/app.js"></script>
</head>
<body ng-controller="MainController">
Name: {{user.login}}
</body>
</html>
And this is my JavaScript Code.
var MainController = function($scope, $http) {
var onUserComplete = function(response) {
$scope.user = response.data;
};
$http.get("https://api.github.com/users/sfshameem5")
.then(onUserComplete);
$scope.message = "Hello, Angular!";
}
I'm expecting {{user.login}}
to show a name, but that doesn't appear.
Using $http Get
$http({
method:"get",
url:"https://api.github.com/users/sfshameem5"
}).success(function(resp){
$scope.user=resp.data
}).error(function(error){
console.log(error)
})
Here. This will work: You should do:
$scope.user = response;
and not
$scope.user = response.data;
The code bellow is working.
HTML:
<!DOCTYPE html>
<html ng-app="app">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://code.angularjs.org/angular-1.0.1.js"></script>
</head>
<body ng-controller="MainController">
Name: {{user.login}}
</body>
</html>
And for your js:
var app = angular.module('app', []);
app.controller('MainController', function($scope, $http) {
$http({
method: "GET",
url: "https://api.github.com/users/sfshameem5",
})
.success(function(response) {
$scope.user = response;
})
.error(function(response) {
console.log("Failed fetching data:" + response);
});
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.