我剛剛部署了一個網站來測試「生產」,但是當我嘗試去網站時,我的一些電腦將看不到一個我的ng-repeat的結果有些人會看到。如果我沒有顯示任何內容時進入網站,我會查看源代碼,並看到數組中的每個對象都有ng-repeat,但屏幕上沒有html輸出。這裏是一些我的代碼,當我載入我的控制器:
/**
* Function that send a request to get a list of posts.
* @return {Function} A promise.
*/
function retrievePosts() {
var defered = $q.defer();
// If the user is logged in we do a search by country, otherwise we get all the posts.
if($rootScope.user !== null && $rootScope.user !== undefined) {
PostService.searchPost({ countries: [$rootScope.user.country] }, function(err, posts) {
if(err) {
defered.reject(err);
}
else if(posts && posts.length > 0) {
defered.resolve(posts);
}
// If the previous condition is not true, we try to get all the posts, since the search by country didn't work.
else {
PostService.getAllPosts(function(err, posts2) {
if(err) {
defered.reject(err);
} else {
defered.resolve(posts2);
}
});
}
});
} else {
PostService.getAllPosts(function(err, posts) {
if(err) {
defered.reject(err);
}
else {
defered.resolve(posts);
}
});
}
return defered.promise;
}
此功能用於獲取JSON數組對象的職位。然後我做這樣的事情:
$q.all([retrieveManufacturer(), retrieveCategories(), retrievePosts(), getTotalPosts(), retrieveGalleryPosts()]).then(function(results) {
$scope.manufacturers = results[0];
$scope.categories = results[1];
// Here we must cache the result and slice it, so that angular doesn't render
// a tone of post but 10 at a time.
postCache = results[2];
$scope.numberOfPostsToShow = 10;
$scope.posts = postCache.slice(0, $scope.numberOfPostsToShow);
// Some code to display the proper amount of post for each category.
var i = -1;
var max = results[3].length;
var groupedPostsCount = { };
var group;
while(++i < max) {
group = results[3][i];
// "_id" contains the name of the category.
groupedPostsCount[group._id] = group.count;
}
if(Object.keys(groupedPostsCount).length > 0){
$scope.categoriesPostCount = groupedPostsCount;
}
$scope.galleryPosts = results[4];
// Prepare the $scope.galleryPosts to be bound with posts.
buildGallery($scope.galleryPosts);
}, function(err) {
console.log(err);
});
$ q.all中的每個任務都被執行,它們全部得到解決。我看到他們在我的HTML,如類別,製造商等...結果[2]這是帖子數組不是他們真的有500個職位在他們。我嘗試在buildGallery()方法調用後調用$ scope。$ apply(),但沒有任何工作。如果我在我的html的任何地方打印{{posts}},我會看到帖子數組。但是,當他們在NG-重複:
<div class="ad-container" ng-repeat="post in posts" ng-click="viewPostDetails(post)">
<div class="ad-picture">
<table class="wrapper">
<tr>
<td><img ng-src="img/175/{{ post.mainImageName || post.imgUrls[0] }}" alt="No image provided"/></td>
</tr>
</table>
</div>
<div class="ad-info">
<span class="ad-info-title">{{ post.title }}</span>
<span class="ad-info-price">{{ post.country == 'Canada' ? (post.price | currency : "CA$") : (post.price | currency : "US$") }}</span>
<br />
<span>{{ post.country }}, {{ post.province }}, {{ post.createdAt | date }}</span>
<p>{{ post.description }}</p>
</div>
</div>
當然這個代碼是綁定了it.Like我說,這真是奇怪的控制器的DIV中。在我的開發電腦上,一切都很完美,但我朋友的一些電腦確實有效,而其他電腦卻沒有。這裏是網站www.firearmsbin.com的鏈接,也許這個問題會發生在你的電腦上。我嘗試了Firefox,firefox的開發,邊緣,鉻和IE11。
感謝。