在不刷新或加载页面的情况下更新 Angular 中的数据
updating data in angular without refreshing or loading the page
>我正在实施一个愿望清单,我想从愿望清单中删除内容,而无需刷新页面即可查看新数据。这是我所做的:
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);
}
}
});
}
似乎您拥有的数据集合正在$scope.wishController
.您需要专门更新此集合并删除已删除的项目:
$http.post('http://localhost:3000/wishlistDel', wish);
$scope.wishController.splice(indexOfDeletedItem, 1);
由于您正在传入$index
因此您需要使用:
$scope.wishController.Themes.splice($index, 1);
通常,管理所有这些的最简单方法是将原始对象从视图传递到delClick
函数中。
如果在ng-repeat
中使用任何筛选器,则依赖使用视图中的$index
是不安全的
这允许您简单地为要从中删除的数组中的对象编制索引。这也意味着您无需重建新对象即可发送到服务器
<div ng-repeat="wish in wishlist">
{{wish.something}}
<button ng-click="delClick(wish)">Delete</button>
在控制器中
$scope.delClick = function(item){
$http.delete("http://localhost:3000/wishlist/"+user.email +'/' + item.id).then(function(){
var index = $scope.wishlist.indexOf(item);
if(index !== -1){
$scope.wishlist.splice(index,1);
}
});
});
您需要
在模板上传递ng-click
的愿望$index
,并在控制器上将其删除,如下所示:
在模板上:
<a ng-click="delClick(title, des, subj, $index)">Delete {{wish.title}}</a>
在控制器上,您需要更改函数才能接收此内容,并从列表模型中删除项:
// Here you'll need to receive the $index from template
$scope.delClick = function(title, des, subj, index) {
// ...
if(delclick==1) {
$http.post('http://localhost:3000/wishlistDel', wish).then(function () {
// Here you'll use the same array of your
// ng-repeat instead $scope.list
$scope.list.splice(index, 1);
});
}
}
编辑
如果您需要使用过滤器,我建议您使用类似的东西或@charlietfl答案
相关文章:
- 如何在不刷新页面的情况下更新显示框
- 获取 HTML 5 音频控制文件位置并在不使用 id 选择器的情况下更新 src
- 在不重定向页面的情况下更新表
- Angular/UI路由器-如何在不刷新所有内容的情况下更新URL
- 如何在不按ctrl+F5的情况下更新我的Web应用程序(chrome、mozilla、IE8+)更改
- 可以在不重新加载页面的情况下更新javascript中的cookie
- 在不重新加载控制器的情况下更新路由
- 在不重新加载页面的情况下更新 URL 哈希
- 在不重新加载页面的情况下更新块中的内容
- 需要 Ajax 函数 - 在不刷新页面的情况下更新数据库
- 在不加载页面的情况下更新谷歌地图上的标记位置
- 在不刷新的情况下更新标签内的变量
- 在不超出页面的情况下更新数据库
- 如何在不使用映射的情况下更新视图模型
- 在不刷新页面的情况下更新 SQL 数据
- 如何在代码点火器中使用 websocket 在不刷新页面的情况下更新内容
- 如何在不刷新浏览器的情况下更新倒计时达到 0 时的日期
- 在不触发状态更改的情况下更新 URL
- 在不重新加载整个页面的情况下更新网页的特定区域
- GULP:如何在不刷新的情况下更新浏览器(仅适用于 CSS 更改)