Javascript 导致错误的角度服务

Javascript 导致错误的角度服务,javascript,angularjs,angularjs-directive,Javascript,Angularjs,Angularjs Directive,我尝试使用$http作为依赖项注入来接收数据,然后使用angular的$q服务将该数据分配给promise。我做错了什么。。似乎找不到在哪里 服务: myApp.factory('githubApi', ['$http', '$q', function($http, $q) { var deferred = $q.defer(); //Declaring a promise that will or will not return a users gith

我尝试使用
$http
作为依赖项注入来接收数据,然后使用angular的
$q
服务将该数据分配给promise。我做错了什么。。似乎找不到在哪里

服务

myApp.factory('githubApi', ['$http', '$q',
    function($http, $q) {
        var deferred = $q.defer();
        //Declaring a promise that will or will not return a users github information.
        this.accountInformation = function (){
            return $http.get('https://api.github.com/users/joshspears3')
            .then(function(response){
                deferred.resolve(response);
                return deferred.promise;
            }, function(response){
                deferred.reject(response);
                return deferred.promise;
            })
        }
    }
]);
myApp.controller('githubCtrl', [ 'githubApi', '$q', '$scope',
    function(githubApi, $q, $scope){
        githubApi.accountInformation()
        .then(
            function (result) {
                $scope.data = result;
            }, function (error) {
                // handle errors here
                console.log(error.statusText);
            }
        );
    }
]);
myApp.directive('githubRequest', [
    function() {
        return {
            scope: {},
            restrict: 'E',
            controller: 'githubCtrl',
            templateUrl: 'public/views/partials/github-request.html'
        }
    }
]);
控制器

myApp.factory('githubApi', ['$http', '$q',
    function($http, $q) {
        var deferred = $q.defer();
        //Declaring a promise that will or will not return a users github information.
        this.accountInformation = function (){
            return $http.get('https://api.github.com/users/joshspears3')
            .then(function(response){
                deferred.resolve(response);
                return deferred.promise;
            }, function(response){
                deferred.reject(response);
                return deferred.promise;
            })
        }
    }
]);
myApp.controller('githubCtrl', [ 'githubApi', '$q', '$scope',
    function(githubApi, $q, $scope){
        githubApi.accountInformation()
        .then(
            function (result) {
                $scope.data = result;
            }, function (error) {
                // handle errors here
                console.log(error.statusText);
            }
        );
    }
]);
myApp.directive('githubRequest', [
    function() {
        return {
            scope: {},
            restrict: 'E',
            controller: 'githubCtrl',
            templateUrl: 'public/views/partials/github-request.html'
        }
    }
]);
指令

myApp.factory('githubApi', ['$http', '$q',
    function($http, $q) {
        var deferred = $q.defer();
        //Declaring a promise that will or will not return a users github information.
        this.accountInformation = function (){
            return $http.get('https://api.github.com/users/joshspears3')
            .then(function(response){
                deferred.resolve(response);
                return deferred.promise;
            }, function(response){
                deferred.reject(response);
                return deferred.promise;
            })
        }
    }
]);
myApp.controller('githubCtrl', [ 'githubApi', '$q', '$scope',
    function(githubApi, $q, $scope){
        githubApi.accountInformation()
        .then(
            function (result) {
                $scope.data = result;
            }, function (error) {
                // handle errors here
                console.log(error.statusText);
            }
        );
    }
]);
myApp.directive('githubRequest', [
    function() {
        return {
            scope: {},
            restrict: 'E',
            controller: 'githubCtrl',
            templateUrl: 'public/views/partials/github-request.html'
        }
    }
]);
部分(github request.html):

这是说当我将我的githubApi服务注入可能是控制器的依赖项时,它是未定义的?

我认为这是因为您的工厂没有返回任何东西

myApp.factory('githubApi', ['$http', '$q',
    function($http, $q) {
       return {
          accountInformation: function (){
            var deferred = $q.defer();
            $http.get('https://api.github.com/users/joshspears3')
            .then(function(response){
                deferred.resolve(response);
            }, function(response){
                deferred.reject(response);
            })
            return deferred.promise;
          }
       }
    }
]);

这是我通常使用的方法

你应该在你的服务方法中返回承诺,延迟对象应该在方法中,而且服务应该返回一个对象:

myApp.factory('githubApi', ['$http', '$q',
    function($http, $q) {
        //Declaring a promise that will or will not return a users github information.
        return {
            accountInformation: function () {
                var deferred = $q.defer();
                $http.get('https://api.github.com/users/joshspears3')
                    .then(function(response){
                        deferred.resolve(response);
                    }, function(response){
                        deferred.reject(response);
                    });
                return deferred.promise;
            }
        }
    }
]);
您还可以简化此操作,因为
$http
服务已经返回承诺:

myApp.factory('githubApi', ['$http',
    function($http) {
        //Declaring a promise that will or will not return a users github information.
        return {
            accountInformation: function () {
                return $http.get('https://api.github.com/users/joshspears3');
            }
        }
    }
]);

首先,
$http.get
已经返回了一个
$q
承诺,不需要再次将其包装在承诺中

其次,工厂创建一个共享的服务实例。因此,工厂的方法应该返回该对象。在factory函数本身中为此分配任何内容都不会公开该方法/属性:

myApp.factory('githubApi', ['$http', function ($http) {
    var githubApi = {
      accountInformation: function () {
          return $http.get('https://api.github.com/users/joshspears3');
      }
    };
    return githubApi;
}

为了获得更多有关错误的详细信息,您是否可以使用angular而不缩小?您是否尝试将
错误:
消息第一行的内容复制到您喜爱的web浏览器中?这些链接中包含的示例可能会对实际问题有所帮助(供将来参考)。@JoshSpears我做了编辑,你应该返回延期。承诺之外的承诺,解析PromiseProvider后没有,仍然会导致错误:未知提供程序:githubApiProvider这是因为函数返回一个对象,该对象声明的属性为=,应为:。@taxicala nice catch,在我将其函数更改为对象属性时没有将其删除