Javascript AngularJS服务未显示
我只是使用Github的API获取我的Github信息。当我记录http请求时,它会显示正确的信息。。我不知道为什么它没有显示在页面上。我没有收到任何错误。(显示的是部分数据,而不是请求的数据) 服务:Javascript AngularJS服务未显示,javascript,angularjs,angularjs-directive,Javascript,Angularjs,Angularjs Directive,我只是使用Github的API获取我的Github信息。当我记录http请求时,它会显示正确的信息。。我不知道为什么它没有显示在页面上。我没有收到任何错误。(显示的是部分数据,而不是请求的数据) 服务: myApp.factory('githubApi', ['$http', function($http) { //Declaring a promise that will or will not return a users github information.
myApp.factory('githubApi', ['$http',
function($http) {
//Declaring a promise that will or will not return a users github information.
return {
async: function() {
return $http.get('https://api.github.com/users/joshspears3');
}
}
}
]);
myApp.controller('githubCtrl', [ 'githubApi', '$scope',
function(githubApi, $scope){
$scope.data = githubApi.async();
}
]);
myApp.directive('githubRequest', [
function() {
return {
scope: {},
restrict: 'E',
controller: 'githubCtrl',
templateUrl: 'public/views/partials/github-request.html'
}
}
]);
<div>
<global-header></global-header>
<div ui-view></div>
<github-request></github-request>
</div>
控制器:
myApp.factory('githubApi', ['$http',
function($http) {
//Declaring a promise that will or will not return a users github information.
return {
async: function() {
return $http.get('https://api.github.com/users/joshspears3');
}
}
}
]);
myApp.controller('githubCtrl', [ 'githubApi', '$scope',
function(githubApi, $scope){
$scope.data = githubApi.async();
}
]);
myApp.directive('githubRequest', [
function() {
return {
scope: {},
restrict: 'E',
controller: 'githubCtrl',
templateUrl: 'public/views/partials/github-request.html'
}
}
]);
<div>
<global-header></global-header>
<div ui-view></div>
<github-request></github-request>
</div>
指令:
myApp.factory('githubApi', ['$http',
function($http) {
//Declaring a promise that will or will not return a users github information.
return {
async: function() {
return $http.get('https://api.github.com/users/joshspears3');
}
}
}
]);
myApp.controller('githubCtrl', [ 'githubApi', '$scope',
function(githubApi, $scope){
$scope.data = githubApi.async();
}
]);
myApp.directive('githubRequest', [
function() {
return {
scope: {},
restrict: 'E',
controller: 'githubCtrl',
templateUrl: 'public/views/partials/github-request.html'
}
}
]);
<div>
<global-header></global-header>
<div ui-view></div>
<github-request></github-request>
</div>
github request.html(部分):
创建服务。基于github API获取信息
发出$http请求以获取我的个人Github信息。
化身:
用户名:{{data.login}
追随者:{{data.Followers},follower:{{data.follower}
Index.html:
myApp.factory('githubApi', ['$http',
function($http) {
//Declaring a promise that will or will not return a users github information.
return {
async: function() {
return $http.get('https://api.github.com/users/joshspears3');
}
}
}
]);
myApp.controller('githubCtrl', [ 'githubApi', '$scope',
function(githubApi, $scope){
$scope.data = githubApi.async();
}
]);
myApp.directive('githubRequest', [
function() {
return {
scope: {},
restrict: 'E',
controller: 'githubCtrl',
templateUrl: 'public/views/partials/github-request.html'
}
}
]);
<div>
<global-header></global-header>
<div ui-view></div>
<github-request></github-request>
</div>
您在这里没有使用承诺。将异步功能更改为:
return $http.get('https://api.github.com/users/joshspears3').success(function(response) {
return response.data
});
以及您的控制器:
function(githubApi, $scope){
githubApi.async().then(function(data) {
$scope.data = data;
});
}
确保已将承诺依赖项添加到代码中