如何在加载应用程序之前添加搜索功能

时间:2016-11-16 07:54:45

标签: javascript html angularjs node.js ionic-framework

我们正在使用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');
       });    
     };    
  });    
}()); 

1 个答案:

答案 0 :(得分:0)

如果没有搜索内容,您可能需要使用视频API。

libAFNetworking.a