2015-09-01 51 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; 
        }); 


     }); 
    }