我正嘗試在Angular和Node中構建自己的購物車系統。我的控制器處理mongo數據庫中的用戶手推車和本地存儲中的手推車(如果用戶沒有帳戶)。現在,我爲我的購物車控制器創建了一種方法,可以從本地存儲和數據庫中刪除購物車項目。我的角度觀看的對象。Angular如何監視Scope變量的變化
$scope.lineItems
用於使用物品填充購物車。現在我可以成功獲取更新的購物車,但我需要刷新我的頁面。通過觸發更新範圍的函數來更新範圍的最好方法是什麼?
代碼:
if (token) {
var payload = authService.parseToken(token);
dataService.getCart(payload._id).then(function (resolve){
if (resolve.data){
$scope.lineItems = resolve.data;
$scope.deleteLineItem = function(lineitem){
dataService.deleteLineItem([lineitem.lineItemID, payload._id]);
}
}
}, function(reason){
console.log(reason);
});
} else {
if (dataService.retrieveLocal('localCart')){
$scope.lineItems = dataService.retrieveLocal('localCart');
$scope.deleteLineItem = function(lineitem){
var cart = dataService.retrieveLocal('localCart');
var updatedCart = cart.filter(function(item){
if (lineitem.lineItemID != item.lineItemID){
return item
}
})
dataService.storeToLocal('localCart', updatedCart);
}
}
}