简体   繁体   English

以角度更新数据,而无需刷新或加载页面

[英]updating data in angular without refreshing or loading the page

I'm implementing a wishlist and I would like to delete content from the wishlist without needing to refresh the page in order to see the new data. 我正在实现一个愿望清单,我想从愿望清单中删除内容,而无需刷新页面以查看新数据。 Here is what I did: 这是我所做的:

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();
            }

          }

}]);

As you can see in the comments I tried $window.location.reload(); 如您所见,我尝试了$window.location.reload(); but it's like refreshing because the whole page is uploaded again instead of removing one item - like display:none , is there any way to do that? 但这就像刷新一样,因为整个页面被重新上传,而不是删除一项,例如display:none ,有什么办法吗?

edit 编辑

        <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>

update 更新

$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);
    }
}

});

} }

It seems like the collection of data you have is on $scope.wishController . 似乎您拥有的数据集合在$scope.wishController You need to update this collection specifically and remove the deleted item: 您需要专门更新此集合并删除已删除的项目:

$http.post('http://localhost:3000/wishlistDel', wish);
$scope.wishController.splice(indexOfDeletedItem, 1);

Since you are passing in $index you would want to use: 由于您要传入$index您需要使用:

$scope.wishController.Themes.splice($index, 1);

You'll need to pass on template the $index of wish on ng-click and remove it on controller, something like this: 您需要在ng-click上传递希望的$index模板,并在控制器上将其删除,如下所示:

On template: 在模板上:

<a ng-click="delClick(title, des, subj, $index)">Delete {{wish.title}}</a>

On controller, you will need to change your function to receive this, and remove item from your list model: 在控制器上,您将需要更改功能以接收此信息,并从列表模型中删除项目:

// 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);
        });
    }

}

EDIT 编辑

If you need to use filters, I recommend something like this or the @charlietfl answer 如果您需要使用过滤器,建议您使用类似以下内容或@charlietfl的答案

Generally the easiest way to manage all of this is pass the original object into your delClick function from the view. 通常,管理所有这些的最简单方法是将原始对象从视图传递到您的delClick函数中。

Relying on using $index from view is not safe if any filters are used in ng-repeat 如果在ng-repeat中使用了任何过滤器,则仅依靠视图使用$index是不安全的

This allows you to simply index that object within array you want to remove it from. 这使您可以简单地索引要从中删除对象的对象。 It also means you don't need to rebuild new object to send to server 这也意味着您无需重建新对象即可发送到服务器

<div ng-repeat="wish in wishlist">
   {{wish.something}}
  <button ng-click="delClick(wish)">Delete</button> 

In controller 在控制器中

$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);
     }
  });    
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM