【问题标题】:How to add search feature before loading app如何在加载应用程序之前添加搜索功能
【发布时间】:2016-11-16 18:33:30
【问题描述】:

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

【问题讨论】:

  • 您不应在代码中放置敏感信息,例如密钥、ID 或凭据。

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


【解决方案1】:

当没有搜索内容时,你可能需要使用videos api。

https://www.googleapis.com/youtube/v3/videos

【讨论】:

    猜你喜欢
    • 2010-10-24
    • 2011-11-11
    • 2016-09-11
    • 2017-09-02
    • 1970-01-01
    • 2021-07-26
    • 2020-02-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多