Angularjs在动作相同的页面后刷新

时间:2015-12-15 09:36:38

标签: angularjs

我正在尝试在链接点击上加载一个功能,这非常有效。然后angularjs发挥其魔力,直到它到达显示用户反馈的点。我需要在删除项目后刷新页面,但它不会刷新。

这是我的href:

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

这是我的控制器:

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

和我的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>

2 个答案:

答案 0 :(得分:2)

您的$ scope.deleteGroup函数应该从$ scope.groups中删除其目标组,因此ng-repeat的内容会自动更新,不再显示该组。

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

答案 1 :(得分:0)

要使用uiRouter强制刷新页面,您可以使用$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});
  });
};