2015-05-09 80 views
0

這裏是我的控制器代碼:爲什麼我的ng-repeat像對象一樣是空的?

.controller('vidController', ["$scope", "$location", "youtubeAPI", function($scope, $location, youtubeAPI) { 
    if(document.getElementById('query')) { //See Note 1 Below***** 
     var textElement = document.getElementById('query'); 
     textElement.onkeyup=function(e){ 
      if(e.keyCode==13){ //if key pressed is enter button 
       console.log('Looking for ' + textElement.value + ' on YouTube'); 
       youtubeAPI.search(textElement.value); 
       $location.path('/results'); //changes url path to results partial 
       $scope.$apply(); 
      } 
     } 
    }; 
    // Called automatically with the response of the YouTube API request. 
    window.onSearchResponse = function(response) { 
     console.log(response); //Shows entire response data 
     console.log(response.items[0].snippet.title); //shows video title 
     console.log(response.items[0].id.videoId); //shows video Id 
     console.log(response.items[0].snippet.thumbnails.medium.url); //shows video thumbnail 
     $scope.videos = response.items; 
     $scope.$apply(); 
     console.log($scope.videos); //This outputs an object with 5 Objects in it. Proof that the API is picking up something. 
    }; 
}]); 

*注1:我有一個用戶將他們的搜索關鍵詞爲輸入文本類型,然後按下回車來運行其上的搜索,它也改變了地址時,按下回車鍵。也許這是造成一個問題?

這裏是部分:

<ul> 
    <li ng-repeat="video in videos"> 
    hey 
    </li> 
</ul> 

由於我使用的是youtubeAPI,每一個搜索結果帶回5個結果。 console.log($scope.video)在控制檯中顯示了5個對象,所以我知道它正在填充。

但是,每當我進入我的部分頁面並檢查元素時,<ul>標籤都是完全空的。像ng-repeat從未跑過。

爲什麼不能運行?我應該看到「嘿」5次。

編輯:這裏是最後的console.log在我的代碼 enter image description here

EDIT 2控制檯輸出:根據要求,這裏是onSearchResponse在哪裏運行。

angular.module('GameFindr.search', []) 
.factory('youtubeAPI', [function() { 

var youtubeAPI = { }; 

youtubeAPI.search = function(keyword) { 
    // Use the JavaScript client library to create a search.list() API call. 
    var request = gapi.client.youtube.search.list({ 
     part: 'snippet', 
     q: keyword, 
     type: 'video', 
    }); 

    // Send the request to the API server, 
    // and invoke onSearchRepsonse() with the response. 
    request.execute(onSearchResponse); 
    } 

    return youtubeAPI; 
}]); 

編輯3:添加$scope.apply();$scope.videos作品,如果我註釋掉$location.path變化,它停留在同樣的觀點。但是當我改變視圖時它不起作用。這裏是我的路由器:

angular.module('GameFindr', [ 
'ngRoute', 
'GameFindr.controllers', 
'GameFindr.search' 
]) 

.config(['$routeProvider', function($routeProvider) { 
$routeProvider. 
    when('/', { templateUrl: 'home.html', controller: 'vidController' }). 
    when('/results', { templateUrl: 'results.html', controller: 'vidController'}). 
    otherwise({ redirectTo: '/' }); 
}]); 
+0

可能是它在角度外的知識,因此'ng-repeat'沒有更新。在你的'console.log'之前試試'$ scope。$ apply()',看看你得到了什麼。 – Zee

+0

我試過了,它沒有幫助,不幸的是 – YourbrainonCompSci

+0

這個函數('onSearchResponse')在哪裏運行?顯示更多的代碼 –

回答

1

你被值可從回調的NG-重複獲取運行已與初始值,而你的情況是不明確的時候改變回調對象的值,所以。

試試下面的代碼

$scope.videos = response.items; 
$scope.$apply(); 

編號:github上 $範圍$適用()稱爲末,告訴異步事件剛剛發生AngularJS。

+0

我收到了不一致的結果。有時候這是有效的,有時候不會。任何想法發生了什麼? – YourbrainonCompSci

+0

''不一致的結果''您在$ scope.videos的控制檯中獲得了什麼?或任何錯誤? – vinayakj

+0

$ scope.videos總是給我正確的信息,但有時候「嘿」會運行5次,有時卻不會。我在原始文章中添加了更多信息,可能會有用 – YourbrainonCompSci

相關問題