2016-11-16 66 views
1

我們正在開發使用Ionic和AngularJS的YouTube頻道。搜索功能只有在通過api加載視頻後才能使用。如何給出搜索框爲空的條件,然後視頻必須自動加載。如果搜索框中有任何內容,則必須搜索包含與搜索框匹配的標題和說明的特定視頻的所有頻道。如何在加載應用程序之前添加搜索功能

(function() { 

    //Make it expressive by introducing new variable app. App now in global space (bad practics) 
    var app = angular.module('youtubevideo', ['ionic', 'youtube-embed', 'ngCordova', 'ngIOS9UIWebViewPatch']); 

    app.run(function($ionicPlatform) { 
    $ionicPlatform.ready(function() { 
     if(window.cordova && window.cordova.plugins.Keyboard) { 
     cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true); 
     } 
     if(window.StatusBar) { 
     StatusBar.styleDefault(); 
     } 
    }); 
    }); 

    //**** Version 2: Refactory codes to use pull to refresh, and infinite scroll ***// 
app.controller('mycontroller', function($scope, $http, $ionicModal){ 

    $scope.videos = [];  
    $scope.appname = ""; 

    $scope.playerVars = { 
     rel: 0, 
     showinfo: 0, 
     modestbranding: 0, 
    } 

    $scope.nextPageToken = null; 

    $scope.youtubeParams = { 
    key: 'my_secretkey', 
    type: 'video', 
    maxResults: '50', 
    part: 'id,snippet', 
    q: 'iphone', 
    order: 'videoCount', 
    channelId: 'my_secretchannelid'  
    } 

    function loadVideos(params, callback) { 
     $http.get('https://www.googleapis.com/youtube/v3/search', {params: params}).success(function(response){    
     var videos = []; 
     if(response.nextPageToken) { 
      $scope.nextPageToken = response.nextPageToken; 
      console.log ($scope.nextPageToken); 
      console.log(response.items); 
      angular.forEach(response.items, function(child){ 
      videos.push(child); 
      }); 
     } 

     callback(videos);    
     }); 
    } 

    $scope.loadOlderVideos = function() { 
    var params = $scope.youtubeParams; 
    if ($scope.nextPageToken) { 
     params['pageToken'] = $scope.nextPageToken; 
    } 
    loadVideos(params, function(olderVideos){ 
     if (olderVideos) { 
     $scope.videos = $scope.videos.concat(olderVideos); 
     } 
     $scope.$broadcast('scroll.infiniteScrollComplete'); 
    }); 
    }; 

    $scope.loadNewerVideos = function() { 
    var params = $scope.youtubeParams; 
    params['pageToken'] = ''; 
     loadVideos(params, function(newerVideos) { 
     $scope.videos = newerVideos; 
     $scope.$broadcast('scroll.refreshComplete'); 
     });  
    };  
    });  
}()); 
+0

你不應該把敏感的東西,如密鑰,ID或證書與代碼。 –

回答

0

當沒有搜索內容時,您可能需要使用視頻API。

https://www.googleapis.com/youtube/v3/videos 
相關問題