AngularJS 中使用 YoutubeAPI v3 的服务

Services in AngularJS with YoutubeAPI v3

我以前使用过一次服务(仍然不是很擅长)并且我已经遇到了很多问题,所以知道我要问(我认为我的问题很简单AF:))

我需要使用 YoutubeApi v3 获取一些 ChannelDetails。

我的服务:

appApi.factory('ServiceAPI', ['$http', function($http) {

var factory = {};

    factory.channelDetails = function(channelname){
        return $http.get('https://www.googleapis.com/youtube/v3/channels?part=contentDetails&forUsername='+channelname+'&key=AIzaSyDQv-WpATIWLinCB3H_sH4W1sKx7plyvRA')
         .success(function(data) {
           return data;
         })
         .error(function(data) {
           return data;
         });
         }
}]);

然后我的控制器:

var appApi = angular.module('YoutubeAPI', ['ngRoute'])

appApi.controller('youtubeCTRL', ['$scope','$http','$q','ServiceAPI', function ($scope, $http, $q, ServiceAPI) {
    $scope.channel = [];

    //GET Id on channelname
    $scope.saveNewchlName = function () {

        var channelname = $scope.newchlName;

        $scope.channelDetails = function(channelname){

            ServiceAPI.channelDetails(channelname).success(function (data) {

                $scope.newchannelNames = {
                    channelName: $scope.newchlName,
                    channelId: data.items[0].id,
                    playlistId: data.items[0].contentDetails.relatedPlaylists.uploads
                };
                console.log($scope.newchannelNames)
                $http({
                    method: 'POST',
                    url: 'http://localhost:8080/api/resources/channelNames/',
                    data: $scope.newchannelNames,
                    dataType: 'json'
                }).success(function (data) {
                    $scope.channel.push(data);
                    console.log('SUCCESS!');
                    $scope.error = null;
                }).error(function (data, status) {
                    if (status == 401) {
                        $scope.error = "You are not authenticated to Post these data";
                        return;
                    }
                    $scope.error = data;
                });
    });
    }
}

我的问题是我总是遇到注入问题。现在我得到一个上面写着:Provider 'ServiceAPI' must return a value from $get factory method.

我需要在 URL 中实现频道名称以获取具体细节。

这似乎有效。

服务:

    appApi.factory('ServiceAPI', ['$http', function($http) {

    var factory = {};

        factory.channelDetails = function(channelname, success, error){
            var promise = $http.get('https://www.googleapis.com/youtube/v3/channels?part=contentDetails&forUsername='+channelname+'&key=AIzaSyDQv-WpATIWLinCB3H_sH4W1sKx7plyvRA')
             if(success){
               promise.success(success);
             }
             if(error){
               promise.error(error);
             };
             }
             return factory;
    }]);

控制器:

var appApi = angular.module('YoutubeAPI', ['ngRoute'])

appApi.controller('youtubeCTRL', ['$scope','$http','$q','ServiceAPI', function ($scope, $http, $q, ServiceAPI) {
    $scope.channel = [];
    $scope.video = [];


    var pagetokenarr = [];

    //GET Id on channelname
        $scope.saveNewchlName = function () {

            var channelname = $scope.newchlName;

                ServiceAPI.channelDetails(channelname, function(data){

                    $scope.newchannelNames = {
                        channelName: $scope.newchlName,
                        channelId: data.items[0].id,
                        playlistId: data.items[0].contentDetails.relatedPlaylists.uploads
                    };
                    console.log($scope.newchannelNames)
                    $http({
                        method: 'POST',
                        url: 'http://localhost:8080/api/resources/channelNames/',
                        data: $scope.newchannelNames,
                        dataType: 'json'
                    }).success(function (data) {
                        $scope.channel.push(data);
                        console.log('SUCCESS!');
                        $scope.error = null;
                    }).error(function (data, status) {
                        if (status == 401) {
                            $scope.error = "You are not authenticated to Post these data";
                            return;
                        }
                        $scope.error = data;
                    });


        });
    }