這裏是我的控制器代碼:爲什麼我的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在我的代碼
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: '/' });
}]);
可能是它在角度外的知識,因此'ng-repeat'沒有更新。在你的'console.log'之前試試'$ scope。$ apply()',看看你得到了什麼。 – Zee
我試過了,它沒有幫助,不幸的是 – YourbrainonCompSci
這個函數('onSearchResponse')在哪裏運行?顯示更多的代碼 –