我的$ scope值没有更新

时间:2016-11-30 10:26:43

标签: angularjs

如果不使用location.reload

更新的对象就无法使用

您好我将从API中获取对象列表,我必须更新一个值,在模态弹出窗口中更新该值后,我无法在不使用location.reload的情况下获得新的更新值。有没有其他解决方案。我正在使用两个不同的控制器。

Will get list of objects from this
 $scope.groups=response.data

将重复此组,我将为每一行编辑按钮,一旦我点击编辑按钮弹出将打开

$scope.editGroup = function(u) {
    $scope.groupName=u.groupName;
    $scope.groupId=u.groupId;
    ModalService.showModal({
        templateUrl: 'app/components/modal/editGroupDetails.html',
        controller: "ModalController",
        scope: $scope
    }).then(function(modal) {
        modal.element.modal();
        modal.close.then(function(result) {

            $scope.message = "You said " + result;
        });
    });
};

接下来,一旦我编辑了GroupName,我将点击提交

$scope.updateGroupName=function(){
                var data={
                "groupId": $scope.groupId,
                "groupName": $scope.groupName
        }
        url=globalUrlConfig.globalAdminApi+globalUrlConfig.updateGroup+$scope.schoolId;
        $http({
            method: 'PUT',
            url: url,
            data: data,
        }).success(function(response) {
            console.log(response)
            if(response._statusCode==200){
                alert(response.data);

                location.reload();
            }
            else{
                alert("not updated")
            }

        })
    }

不使用location.reload我无法显示更新的数据

1 个答案:

答案 0 :(得分:0)

像这样使用$rootscope

添加此$rootScope.$emit("refreshData", {});  而不是Location.reload();

然后将其添加到主控制器中,以便无需刷新

即可加载数据
var RefreshValue = $rootScope.$on("refreshData", function () {
             GetValueMethod(); // call your method (List of object) for get data without reload form 
        });
        $scope.$on('$destroy', RefreshValue);

不要忘记注入$rootScope