2015-09-01 35 views
0

我曾经使用服务一次(仍然不是很好),我已经遇到很多问题,所以知道我会问(我认为我的问题很容易AF :))AngularJS与YoutubeAPI v3服务

我需要使用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'必须从$ get factory方法返回一个值。

我需要在URL中实现通道名以获取特定的详细信息。

回答

0

这似乎工作。

服务:

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; 
        }); 


     }); 
    }