我正在實施心願單,我想刪除心願單中的內容,而無需刷新頁面以查看新數據。 這裏是我做過什麼:角度更新數據,無需刷新或加載頁面
wish.controller('wishCtrl',['$scope','$http','$cookies','$window',function($scope,$http,$cookies,$window) {
var user={};
user.email = $cookies.get('cookieEmail');
$http.get("http://localhost:3000/wishlist/"+user.email).success(function(data){
$scope.wishController = data;
console.log(data);
});
var delclick=0;
var newView =0;
$scope.delClick = function(title, des, subj) {
var wish = {};
wish.user = $cookies.get('cookieEmail');
wish.sub = subj;
wish.title = title;
wish.description=des;
console.log(wish.user);
console.log(wish.title);
console.log(wish.sub);
console.log(wish.description);
delclick=1;
if(delclick==1)
{
$http.post('http://localhost:3000/wishlistDel', wish).then()
//$scope.load();
//$window.location.reload();
}
}
}]);
正如你在我試圖$window.location.reload();
註釋中看到,但它就像提神,因爲整個頁面再次上傳,而不是刪除一個項目 - 像display:none
,有沒有辦法做到那?
編輯
<div ng-repeat="wishlist in wishController.Themes" >
<h2 class="text-center">{{wishlist.title}}</h2>
<h2 class="text-center">{{wishlist.description}}</h2>
<img ng-src="{{wishlist.image}}" class="imgCenter">
<button class="w3-btn w3-ripple" ng-click="delClick(wishlist.title, wishlist.description, wishlist.sub, $index)">click </button>
</div>
更新
$http.post('http://localhost:3000/wishlistDel', wish).then(function() {
if(item.sub=='party'){
var index = $scope.wishController.Party.indexOf(item);
console.log(index);
if(index !== -1){
$scope.wishController.Party.splice(index,1);
}
}
if(item.sub=='activity'){
var index = $scope.wishController.Activity.indexOf(item);
console.log(index);
if(index !== -1){
$scope.wishController.Activity.splice(index,1);
}
}
if(item.sub=='theme'){
var index = $scope.wishController.Themes.indexOf(item);
console.log(index);
if(index !== -1){
$scope.wishController.Themes.splice(index,1);
}
}
});
}
可以肯定的是在原來的物體通過從視圖 – charlietfl
是什麼目的簡化這個設置'delclick = 1;'然後立即測試'if(delclick == 1)'? – Lex
您在ng-click上添加了$ index,但在您的功能中沒有。 – rneves