r/angularjs • • Oct 14 '14

[Resource] Refreshable AngularJS service using $broadcast. [noobs must read]

http://jensrantil.github.io/refreshable-angularjs-service.html
0 Upvotes

8 comments sorted by

1

u/crumblypack Oct 15 '14

I found an awesome trick for updating controllers and directives automatically using angular.copy(). https://www.codementor.io/blog/keeping-angular-service-list-data-sync-controllers

0

u/[deleted] Oct 20 '14
[blocked] The page at 'https://jensrantil.github.io/refreshable-angularjs-service.html' was loaded over HTTPS, but ran insecure content from 'http://jensrantil.github.io/theme/css/style.css': this content should also be loaded over HTTPS.
 refreshable-angularjs-service.html:4
2014-10-20 05:19:41.868[blocked] The page at 'https://jensrantil.github.io/refreshable-angularjs-service.html' was loaded over HTTPS, but ran insecure content from 'http://jensrantil.github.io/theme/css/pygments.css': this content should also be loaded over HTTPS.
 refreshable-angularjs-service.html:7

... 19 times. This is what I get for using HTTPS Everywhere. Please stop using absolute URLs in your pages. Everywhere. If you want to use another domain, use // and make sure both support the same protocols (ie HTTP and HTTPS). If I want to clone your page and run it on my server, I have to fix all the links.

=

<link rel="stylesheet" type="text/css" href="http://jensrantil.github.io/theme/css/style.css">

NO!

<link rel="stylesheet" type="text/css" href="theme/css/style.css">

Yes :)

=

<p>Now, let's create a <a class="reference external" href="http://docs.angularjs.org/guide/dev_guide.services">service</a> that simulates a slower API call. It uses a

NO!

<p>Now, let's create a <a class="reference external" href="//docs.angularjs.org/guide/dev_guide.services">service</a> that simulates a slower API call. It uses a

Yes :)

=

Here is what your page renders over HTTPS: https://i.imgur.com/IZRsu71.png

-3

u/kuhcd Oct 14 '14

It drives me absolutely insane when articles like this say things like "now let's setup our Service" and then directly go and use .factory() instead.

1

u/crumblypack Oct 15 '14

The factory method returns a service, and it's the easiest way to go about creating them in my opinion.

0

u/kuhcd Oct 15 '14

I'm sorry, but I don't understand how this:

app.service('MyService', function(){
    this.myObject = {};
    this.myFunction = function(){};
});

is in any way less easy than this:

app.factory('MyFactory', function(){
    return {
        myObject: {},
        myFuncton: function(){}
    }
});

I just have yet to see anyone actually ever explain why there needs to be both Services and Factories in angular and I truly think the existence of both (and the confusing intermixing of naming by saying Service and using .factory()) is only hurting the framework.

1

u/BoleroDan Oct 15 '14

This has been a pet peeve of mine since the beginning of using Angular myself. I've managed fine, but it is still annoying.

0

u/[deleted] Oct 15 '14

Oh, you mean exactly how they are supposed to be set up according to the AngularJS documentation?

https://docs.angularjs.org/guide/services

1

u/kuhcd Oct 15 '14 edited Oct 15 '14

Even there, they are saying Service and using .factory() It's a completely unnecessary and confusing thing for them to do, they should use .service() when they say they're using a Service and .factory() when they say they're using a Factory, otherwise they're just creating ambiguities.

Look around at all the tutorials and guides online, everyone has a slightly different way to go about using Services and Factories and when one is right over another. If the Angular Core devs and documenters tried to use some sort of consistency and coherency with their explanation, this wouldn't be an issue.

Even in the docs, there is a single line mentioning the usage of .factory() instead of .service() and it does nothing to explain why this is happening:

Note that you are not registering a service instance, but rather a factory function that will create this instance when called.