0

I'm trying to load a function on a link click, which works perfectly. Then angularjs does its magic until it arrives on the point where it shows the user feedback. I need to refresh the page after deleting an item, but it simply won't refresh.

Here's my href:

<a ng-click="deleteGroup(group.id)" target="_self">
<img src="img/deleteGroupIcon.png" width="45px"/></a>

here's my Controller:

$scope.deleteGroup = function ($groupId) {
    $http({
        method: 'POST',
        url: apiUrl + 'group/delete',
        data: {'groupId': $groupId},  // pass in data as strings
        //headers: {'Content-Type': 'application/x-www-form-urlencoded'}  
        // set the headers so angular passing info as 
        // form data (not request payload)
    })
    .success(function (data) {
        if (!data.success) {
            //$route.reload();
            alert('groep is verwijderd');
        } else {
            $scope.group = data;
            //$state.go('userDetail', {'user_id' : $userId});
        }
    });
};

and my html:

<div class="searchResults" ng-repeat="group in groups | searchForUser:searchString">
<div class="manageGroupWrapper">
    <a class="normal" href="#">
        <div class="newName h3">
            {{group.title}}
        </div>
        <div class="newProfile">
            <img ng-src="{{group.image}}" width="200px"/>
        </div>
        <div class="imageWrapper">
            <a ui-sref="add_group" class="editGroupIcon"><img src="img/editGroupIcon.png" width="50px"/></a>
            <a ng-click="deleteGroup(group.id)" target="_self"><img src="img/deleteGroupIcon.png" width="45px"/></a>
        </div>
    </a>
</div>
7
  • 2
    Why do you want to refresh the page? Could you give us more of your HTML source? Because, if you're using ng-repeat you could just remove the group which you just deleted from the array which you ng-repeat over and avoid refreshing the page in that way. AngularJS will handle the databinding and remove the group from the view as well. (this is handled by ng-repeat) Commented Dec 15, 2015 at 9:39
  • 1
    Are you using Angular routing or normal web pages (is there a # symbol in your URLs)? The alert is shown? Normally you shouldn't be reloading a page in a Angular application, just update the model Commented Dec 15, 2015 at 9:39
  • What about wrapping the delete in success in a $timeout. Commented Dec 15, 2015 at 9:41
  • ui router, yes the alert is shown, if the page is not refreshed, the product is not deleted until i press F5 Commented Dec 15, 2015 at 9:41
  • I am using ng-repeat, are there extra steps needed? because right now it does not delete the product until F5 Commented Dec 15, 2015 at 9:43

2 Answers 2

2

Your $scope.deleteGroup function should remove its target group from the $scope.groups so the content of the ng-repeat is automatically updated and does not display the group anymore.

$scope.deleteGroup = function (group) {
    $http({
        method: 'POST',
        url: apiUrl + 'group/delete',
        data: {'groupId': group.$groupId},  // pass in data as strings
        //headers: {'Content-Type': 'application/x-www-form-urlencoded'}  
        // set the headers so angular passing info as 
        // form data (not request payload)
    }).success(function(data) {
        $scope.groups.splice($scope.groups.indexOf(group));
    });
};
Sign up to request clarification or add additional context in comments.

Comments

0

To force refreshing the page with uiRouter you can use $state.go(target, params, {reload: true}).

$scope.deleteGroup = function (group) {
  $http({
    method: 'POST',
    url: apiUrl + 'group/delete',
    data: {'groupId': group.groupId},  // pass in data as strings
    //headers: {'Content-Type': 'application/x-www-form-urlencoded'}  
    // set the headers so angular passing info as 
    // form data (not request payload)
  }).success(function(data) {
    // This will reload the current state with the same stateParams
    $state.go($state.current.name, $stateParams, {reload: true});
  });
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.