1

While creating my app in AngularJS (awesome framework!) I stuck in one task: how to show and hide hidden div (ng-show) after some action.

Detailed description: using AngularUI $modal service I'm asking if user wants to perform action, if yes, I run service to POST request via $http to send which item I want to delete. After it finished I want to show hidden div with information, that process has accomplished successfully. I created a simple service with $timeout to set div's ng-show and hide after some time but it doesn't update variable assigned to ng-show directive. Here is some code:

Controller for listing and deleting items

 $scope.deleteSuccessInfo = false; //variable attached to div ng-show

 $scope.deleteCluster = function(modalType, clusterToDelete) {

    modalDialogSrvc.displayDialog(modalType)
        .then(function(confirmed) {

            if(!confirmed) {
                return;
            }

            clusterDeleteSrvc.performDelete(itemToDelete)
                .then(function(value) {
                    //my attempt to show and hide div with ng-show
                    $scope.deleteSuccessInfo = showAlertSrvc(4000);
                    updateView(itemToDelete.itemIndex);
                }, function(reason) {
                    console.log('Error 2', reason);
                });

        }, function() {
            console.info('Modal dismissed at: ' + new Date());
        });
};

function updateView(item) {
   return $scope.listItems.items.splice(item, 1);
}

Part of service for deleting item

function performDelete(itemToDelete) {

    var deferred = $q.defer(),
        path = globals.itemsListAPI + '/' + itemToDelete.itemId;

    getDataSrvc.makeDeleteRequest(path)
        .then(function() {
            console.log('Item removed successfully');
            deferred.resolve({finishedDeleting: true});
        }, function(reason) {
            console.log('error ', reason);
            deferred.reject(reason);
        });

    return deferred.promise;
}

return {
    performDelete: performDelete
};

Simple service with $timeout to change Boolean value after some time

angular.module('myApp')
    .service('showAlertSrvc', ['$timeout', function($timeout) {

        return function(delay) {

            $timeout(function() {
                return false;
            }, delay);

            return true;
        };

    }]);

I tried $scope.$watch('deleteSuccessInfo', function(a, b) {...}) with no effect. How to apply false after some delay? Or maybe You would achieve this in other way?

Thank You in advance for any help!

1 Answer 1

1

Change the implementation of the showAlertSrvc service, like this:

angular.module('myApp')
 .service('showAlertSrvc', ['$timeout', function($timeout) {
        return function(delay) {
            var result = {hidden:true};
            $timeout(function() {
                result.hidden=false;
            }, delay);
            return result;
        };
    }]);

And then change thes 2 lines:

The declaration of deleteSuccessInfo should be like this:

 $scope.deleteSuccessInfo = {}; 

And then do this:

$scope.deleteSuccessInfo = showAlertSrvc(4000);

And finally in your view do this "ng-show=!deleteSuccessInfo.hidden"

Example

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

9 Comments

Thank You for the solution Josep. Anyway I was hoping for creating a reusable showAlertSrvc service, which I be able to use easy with other controllers like creating items.
@cachaito I will updated my answer in order to explain why that service is not worth it, please check my answer in 5 minutes. There is a very good reason for not having that service.
I will wait @Josep :-) Meanwhile I did read $q docs and I discovered a third callback in retunredPromise.then(successCallback, errorCallback, notifyCallback); What would You say to set: $scope.deleteSuccessInfo = true in successCallback and set $timeout with $scope.deleteSuccessInfo = false in notifyCallback?
@cachaito 2 things: 1) I just re-updated my answer, sorry, have a look at that please. 2) The reason is because I thought that the implementation of the service would end up being a wrapper of $timeout or falling into a very common anti-pattern the deferred anti-pattern
@cachaito ok, check the latest update. I've made this jsFiddle to prove that it works. Please let me know if this is what you wanted. Thanks!
|

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.