1

I have a save function in which the changes are saving and after the success response of save I am calling a get function which fetches latest updated data from DB. But the changes are not reflecting in the view.

What is the solution for this? code is like this

 $scope.saveMenu = function () {
        $('.save-button').prop('disabled', true);
        $http({
            url: '/api/menu/save',
            method: 'POST',
            data: 'aff=' +JSON.stringify($scope.aff) + '&menuAccess='+ JSON.stringify($scope.menuAccess) + '&brokerId=' + $scope.filter.business_filter,
            headers:{'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'}
        }).success(function (response) {
            var oResponse = angular.fromJson(response);
            $('.save-button').prop('disabled', false);
            if (oResponse.success) {
                $scope.$apply(function() {
                    $scope.getUsers();
                });

                $('.save').fadeIn();
                setTimeout(function () {
                    $('.save').fadeOut();
                }, 2000);

            } else {
                $window.location.href = $rootScope.ng_url + '/error/' + oResponse.status_code;
            }
        });
    }

    $scope.getUsers = function () {
        $scope.filter.view_group_filter = ($scope.filter.view_group_filter != '') ? $scope.filter.view_group_filter : 0;

        if ($scope.filter.business_filter != -5) {
            $('#group').attr('disabled', true);
        } else {
            $('#group').attr('disabled', false);
        }

        $http.get('/api/menu/users/' + $scope.filter.business_filter + '/' + $scope.filter.view_group_filter)
            .then(
                function (response) {
                    // success callback
                    var oResponse = angular.fromJson(response.data);
                    if (oResponse.success) {
                        $scope.aff = oResponse.data.Aff;
                    } else {
                        $window.location.href = $rootScope.ng_url + '/error/' + oResponse.status_code;
                    }
                }
            );
    }
3
  • 4
    It will be complicated to answer without a piece of code Commented Oct 5, 2016 at 10:25
  • Code sample added Commented Oct 5, 2016 at 10:54
  • A thing: you need to call jquery events in a $timeout()... not is the same thing of your setTimeout. Commented Oct 5, 2016 at 19:09

1 Answer 1

1

$apply brings you into angular context. No need to use $scope.$apply as you are using $http, you are already in angular context.

$scope.$apply(function() {
  $scope.getUsers();
});

Change this to,

$scope.getUsers();

Here, setTimeout not updating any scope variable, no harm. But if it is updating any scope data. Instead of using setTimeout use $timeout which internally calls $apply and hence $digest.

Sign up to request clarification or add additional context in comments.

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.