Re: [angular.js] Create a service which extends $http (or any other service)

4,325 views
Skip to first unread message

Peter Bacon Darwin

unread,
Jul 19, 2012, 11:28:58 AM7/19/12
to ang...@googlegroups.com
How about composition?  Write a service that has $http injected into it and uses it.  This is what ngResource does.
Pete

On 19 July 2012 14:58, Dario Gieselaar <d.gie...@gmail.com> wrote:
Hi,

Is it possible to define a service which extends $http (or any other service)? Normally I would just use angular.extend(), but I have no idea what the second argument would have to be, as $http will be undefined.

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To view this discussion on the web visit https://groups.google.com/d/msg/angular/-/6UeU-FHofxoJ.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/angular?hl=en.

Alec Taylor

unread,
Jul 21, 2012, 9:56:30 AM7/21/12
to ang...@googlegroups.com
What's the protocol you're trying to add support for?

On Sat, Jul 21, 2012 at 11:35 PM, Dario Gieselaar <d.gie...@gmail.com> wrote:
Well, I want all my controllers to use this custom $http (let's call it customHttp) service for future changes (rerouting requests, debugging, etc). Suppose functionality is added to the $http service which is the best choice for a certain controller. To use this functionality I would then also have to define this method in customHttp. If I use inheritance instead of composition, I would not have to worry about new functionality in $http.

On Thursday, July 19, 2012 5:28:58 PM UTC+2, Peter Bacon Darwin wrote:
How about composition?  Write a service that has $http injected into it and uses it.  This is what ngResource does.
Pete

On 19 July 2012 14:58, Dario Gieselaar <d.gie...@gmail.com> wrote:
Hi,

Is it possible to define a service which extends $http (or any other service)? Normally I would just use angular.extend(), but I have no idea what the second argument would have to be, as $http will be undefined.

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To view this discussion on the web visit https://groups.google.com/d/msg/angular/-/6UeU-FHofxoJ.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/angular?hl=en.

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To view this discussion on the web visit https://groups.google.com/d/msg/angular/-/rZ6lEYxgUrIJ.

Pawel Kozlowski

unread,
Jul 21, 2012, 12:23:15 PM7/21/12
to ang...@googlegroups.com
Hi!

On Sat, Jul 21, 2012 at 4:05 PM, Dario Gieselaar <d.gie...@gmail.com> wrote:
> Assuming you're referring to a network protocol, none. All I'm currently
> doing is rewrite urls on the client side, and I've added some log
> statements.

If its about rewriting URLs and some logging logic a decorator would
be a perfect place to inject your logic:
http://docs.angularjs.org/api/AUTO.$provide
This way other parts of the code could still refference $http as its
dependency yet still get a decorated instance.

Cheers,
Pawel

Peter Bacon Darwin

unread,
Jul 21, 2012, 11:11:19 AM7/21/12
to ang...@googlegroups.com

That doesn't work with dependency injection. You could try decorating the service though if you wanted to modify the way $http works.

Pete
...from my mobile.

On Jul 21, 2012 4:07 PM, "Andy Joslin" <andyt...@gmail.com> wrote:
Hi Darlo,

Well, remember it's just plain old Javascript:

myHttp = function() {
}
myHttp.prototype = new $http();

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To view this discussion on the web visit https://groups.google.com/d/msg/angular/-/fzjyvutMT3kJ.

Georgios Diamantopoulos

unread,
Jul 25, 2013, 5:34:56 AM7/25/13
to ang...@googlegroups.com
Guys,

I'm trying to decorate $route so I can add some of my own methods there but I'm getting a circular dependency:

Error: Circular dependency: $route <- ngViewDirective

Any ideas?

Thanks
Georgios

Jose Luis Rivas

unread,
Jul 25, 2013, 5:38:18 AM7/25/13
to ang...@googlegroups.com
On 7/25/13 2:34 AM, Georgios Diamantopoulos wrote:
> Guys,
>
> I'm trying to decorate $route so I can add some of my own methods there
> but I'm getting a circular dependency:
>
> Error: Circular dependency: $route <- ngViewDirective
>
> Any ideas?

How's your code looking?

--
Jose Luis Rivas
http://joseluisrivas.net/

Georgios Diamantopoulos

unread,
Jul 25, 2013, 5:46:00 AM7/25/13
to ang...@googlegroups.com
This is after trying to separate into a different module in case  that was the source of the problem:

var otherModule = angular.module('foo', []);
    otherModule.config(['$provide', function($provide) {
        $provide.decorator('$route', function ($route) {
            return $route;
        });
    }]);


    var mainModule = angular.module('i', ['foo']);



> Date: Thu, 25 Jul 2013 02:38:18 -0700
> From: ghost...@gmail.com
> To: ang...@googlegroups.com
> Subject: Re: [AngularJS] Re: [angular.js] Re: Create a service which extends $http (or any other service)
> --
> You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/72ukcZYeWGE/unsubscribe.
> To unsubscribe from this group and all its topics, 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.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Jose Luis Rivas

unread,
Jul 25, 2013, 5:53:47 AM7/25/13
to ang...@googlegroups.com
On 7/25/13 2:46 AM, Georgios Diamantopoulos wrote:
> This is after trying to separate into a different module in case that
> was the source of the problem:
>
> var otherModule = angular.module('foo', []);
> otherModule.config(['$provide', function($provide) {
> $provide.decorator('$route', function ($route) {
> return $route;
> });
> }]);
>
>
> var mainModule = angular.module('i', ['foo']);
>

Shouldn't be using $delegate instead of $router?

Like in http://blog.omkarpatil.com/2013/04/using-angularjs-decorators.html

Georgios Diamantopoulos

unread,
Jul 25, 2013, 5:56:25 AM7/25/13
to ang...@googlegroups.com
That's an injection-ready func? Good God... I copied the example posted above which used $http so hence the error.

Thanks!


> Date: Thu, 25 Jul 2013 02:53:47 -0700

> From: ghost...@gmail.com
> To: ang...@googlegroups.com
> Subject: Re: [AngularJS] Re: [angular.js] Re: Create a service which extends $http (or any other service)
>
Reply all
Reply to author
Forward
0 new messages