0

i like to show an alert box every time a request is done. To shorten my way i like to use the 'Bootstrap Alert'. There seems to be no way to access on any scope inside a factory. How can i realize something like this ?

app.factory('httpInterceptor', function ($q) {
    return {
        // On request success
        request: function (config) {
            $scope.alerts.push({msg: "Request done !"}); //There is no scope
            return config || $q.when(config);
        },
        // On request failure
        requestError: function (rejection) {
            return $q.reject(rejection);
        },
        // On response success
        response: function (response) {
            return response || $q.when(response);
        },
        // On response failure
        responseError: function (rejection) {
            return $q.reject(rejection);
        }
    };
});

See Alert Example on http://angular-ui.github.io/bootstrap/

1
  • 2
    I think you can inject $rootScope here. Try it. Use the $rootScope.$broadcast method to raise an event and catch it in some controller to show the later. Commented Dec 10, 2013 at 10:04

2 Answers 2

1

You can create a service that will be injected into the interceptor and the scope:

.factory('alerts',

function () {

    var alerts = [];

    return {

        getAlerts: function () {
            return alerts;
        },

        addAlert: function (msg) {
            alerts.push({ msg: msg });
        },

        closeAlert: function (index) {
            alerts.splice(index, 1);
        }
    }
 }) 

In the controller of the scope use it like that:

function($scope, alerts) {
    $scope.alerts = alerts.getAlerts();
}

And in the interceptor itself:

alerts.addAlert('msg');
Sign up to request clarification or add additional context in comments.

Comments

1

I know this question is quite old and perhaps the OP has found better solution, but i am providing the answer here as a reference to those who have not found a solution.

We can make use of $broadcast method available with bootstrap. Read more about $broadcast here : https://docs.angularjs.org/api/ng/type/$rootScope.Scope#$broadcast

app.factory('httpInterceptor', function ($q) {
    return {
        // On request success
        request: function (config) {
            $rootScope.$broadcast("requestDone");               
            return config || $q.when(config);
        },
        // On request failure
        requestError: function (rejection) {
            return $q.reject(rejection);
        },
        // On response success
        response: function (response) {
            return response || $q.when(response);
        },
        // On response failure
        responseError: function (rejection) {
            return $q.reject(rejection);
        }
    };
});

In your controller, just listen for the event as :

$rootScope.$on("loginStatus", function(e) {
  $scope.alerts.push({msg: "Request done !"}); //There is scope  
});

Cheers!!

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.