我有一個JSON輸出items
- 要顯示我使用ng-repeat="item in items"
的單個項目。我可以使用user
帶有JSON數組的ng-if indexOf
訪問當前登錄的用戶對象每個項目可以屬於多個用戶的願望清單。如果用戶添加一個項目到他的心願的user_id
被保存的內item.wishlists
單item
的JSON輸出看起來簡單的東西是這樣的:
{"id":1,"title":"This is a tile","wishlists":[{"user_id":2},{"user_id":3}]}
當我這樣做user.id
我得到的電流ID登錄用戶。
現在我想用ng-if
的ng-repeat="item in items"
內檢查CURRENT_USER已經將本項目到他的心願,然後顯示該用戶的「刪除願望清單」按鈕,而不是「加入收藏」。
我不工作的做法是:
<div ng-repeat="item in items">
<h3>{{item.title}}</h3>
<a href="" ng-click="addToWishlist(item)" ng-if="item.wishlists.indexOf(user.id) == -1">Wish</a>
<a href="" ng-click="removeFromWishlist(item)" ng-if="item.wishlists.indexOf(user.id) > -1">Undo-Wish</a>
</div>
我可以使用的indexOf這樣嗎?或者什麼是正確的angularJS方法來檢查user.id是否在item.wishlists user_id的內?
Update:
查看:(ctrl.hasWished改變從圖標 '收藏' 如果真要 'favorite_border' 如果假)
<div ng-repeat="item in items">
<md-button class="md-icon-button feed-wish md-warn" aria-label="Wish" ng-click="ctrl.toggleWish(item)">
<i class="material-icons">favorite{{ctrl.hasWished(item) ? '' : '_border' }}</i>
</md-button>
</div>
控制器:
// Check if User has wished this item already: Should return true or false
this.hasWished = function(item) {
return item.wishlists.some(function(wishlist) {
return wishlist.user_id === Auth._currentUser.id; // returns true if currently logged in user id is on wishlist
});
};
this.toggleWish = function(item) {
if (!this.hasWished) {
this.hasWished = true;
items.wish(item); // sends PUT request
ToastService.show(item.product + ' added to Wish List');
} else {
this.hasWished = false;
items.unWish(item); // sends DELETE request
ToastService.show(item.product + ' removed from Wish List');
}
}
當我點擊按鈕時,如果它之前爲false,它會發送PUT請求,我也會得到ToastSer副消息「添加..」,如果我再次點擊它發送DELETE請求和ToastService消息「刪除..」。但圖標不會從「最喜歡的」改變爲「favorite_border」。當我重新加載頁面時,顯示正確的圖標。
我也有在控制檯中此錯誤消息:
TypeError: Cannot read property 'id' of null
at http://ruby-on-rails-140223.nitrousapp.com:3000/assets/auctions/auctionCtrl-f2cb59b8ad51e03a2264ef2c6e759a54.js?body=1:33:52
at Array.some (native)
at hasWished (http://ruby-on-rails-140223.nitrousapp.com:3000/assets/auctions/auctionCtrl-f2cb59b8ad51e03a2264ef2c6e759a54.js?body=1:32:30)
at fn (eval at <anonymous> (http://ruby-on-rails-140223.nitrousapp.com:3000/assets/angular/angular-81b9b668c3f93c8ccb76b27ad984d9b6.js?body=1:13323:15), <anonymous>:4:318)
at Object.expressionInputWatch [as get] (http://ruby-on-rails-140223.nitrousapp.com:3000/assets/angular/angular-81b9b668c3f93c8ccb76b27ad984d9b6.js?body=1:14303:31)
at Scope.$digest (http://ruby-on-rails-140223.nitrousapp.com:3000/assets/angular/angular-81b9b668c3f93c8ccb76b27ad984d9b6.js?body=1:15819:40)
at Scope.$apply (http://ruby-on-rails-140223.nitrousapp.com:3000/assets/angular/angular-81b9b668c3f93c8ccb76b27ad984d9b6.js?body=1:16098:24)
at done (http://ruby-on-rails-140223.nitrousapp.com:3000/assets/angular/angular-81b9b668c3f93c8ccb76b27ad984d9b6.js?body=1:10547:47)
at completeRequest (http://ruby-on-rails-140223.nitrousapp.com:3000/assets/angular/angular-81b9b668c3f93c8ccb76b27ad984d9b6.js?body=1:10745:7)
at XMLHttpRequest.requestLoaded (http://ruby-on-rails-140223.nitrousapp.com:3000/assets/angular/angular-81b9b668c3f93c8ccb76b27ad984d9b6.js?body=1:10686:9)
也可以點擊此錯誤時:
TypeError: v2.hasWished is not a function
at fn (eval at <anonymous> (http://ruby-on-rails-140223.nitrousapp.com:3000/assets/angular/angular-81b9b668c3f93c8ccb76b27ad984d9b6.js?body=1:13323:15), <anonymous>:4:318)
at Object.expressionInputWatch [as get] (http://ruby-on-rails-140223.nitrousapp.com:3000/assets/angular/angular-81b9b668c3f93c8ccb76b27ad984d9b6.js?body=1:14303:31)
at Scope.$digest (http://ruby-on-rails-140223.nitrousapp.com:3000/assets/angular/angular-81b9b668c3f93c8ccb76b27ad984d9b6.js?body=1:15819:40)
at Scope.$apply (http://ruby-on-rails-140223.nitrousapp.com:3000/assets/angular/angular-81b9b668c3f93c8ccb76b27ad984d9b6.js?body=1:16098:24)
at HTMLButtonElement.<anonymous> (http://ruby-on-rails-140223.nitrousapp.com:3000/assets/angular-touch/angular-touch-b4d02ed602708a1c93b51087222e0363.js?body=1:478:13)
at HTMLButtonElement.eventHandler (http://ruby-on-rails-140223.nitrousapp.com:3000/assets/angular/angular-81b9b668c3f93c8ccb76b27ad984d9b6.js?body=1:3299:21)
嘗試使用'(item.wishlists |過濾器:{USER_ID:user.id})。長度== 0' –