how to get $rootScope in transformRequest of $httpProvider

1,404 views
Skip to first unread message

Martin Kuhn

unread,
Feb 6, 2013, 3:07:11 PM2/6/13
to ang...@googlegroups.com
Hi,

I would like to enhance the header data of http calls. The data exists in $rootScope. So the approach is done via a function passed to transformRequest array of $httpProvider:

My Code:

angular.module('services.http.intercepting', [])
    .config(function ($httpProvider) {

        var enhanceHeader = function (data, headersGetter) {
            // TODO: how to get $rootScope in function ?????

            var headers = headersGetter();
            if (headers && $rootScope.xyz {
                headers.xyz = $rootScope.xyz;
            }

            return data;
        };
    $httpProvider.defaults.transformRequest.push(enhanceHeader);
})

Can anybody of you give me a hint??

TIA
Martin

Peter Bacon Darwin

unread,
Feb 6, 2013, 3:25:10 PM2/6/13
to ang...@googlegroups.com
I don't think you can as transformRequests are not injectable.


Martin

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular?hl=en-US.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Spiros Kabasakalis

unread,
May 1, 2013, 5:03:26 PM5/1/13
to ang...@googlegroups.com

I am trying to do the same thing.I want to broadcast an event to start a spinner.Have registered a listener on $rootScope,that will start the spinner.
I tried to injectlike this,but no luck

      $httpProvider.defaults.transformRequest.push([
             '$rootScope', function( $rootScope) {
   return        function(data, headersGetter) {
                          $rootScope.$broadcast('spinner');
                            console.log('$rootScope in httpconfig',$rootScope);
                          return data;
                         }
            }
                 ] );

Stephan Malek

unread,
Jun 7, 2013, 7:54:22 AM6/7/13
to ang...@googlegroups.com
Hello,

its possible:

        // ~~ [HTTP TRANSFORM REQUEST]
        var ajaxSpinner = function (data, headersGetter) {
            var $injector  = angular.element('.my-parent-class').injector(),
                $rootScope = $injector.get('$rootScope');

            // show ajax loading
            $rootScope.ajaxLoading = true;

            return data;
        };

greetz stephan
Reply all
Reply to author
Forward
0 new messages