Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

8
  • 1
    A perfect answer. I wonder if any angular googlers can offer and better solution? Certainly for services such as logging and analytics this is very important. Commented Sep 11, 2013 at 8:55
  • 1
    Is there a way to amend this so that the binding is added inside the service definition? Commented Sep 11, 2013 at 9:01
  • 5
    @sidonaldson This is generally considered an anti-pattern in the angular world. Because it makes for a less testable environment (due to the assumption that the service is always available on a scope). That's why you don't see many posts on this topic. Commented Nov 19, 2013 at 17:44
  • 7
    @rob - According to best practices, the factory should be injected in to the controllers that need to use it, rather than on the root scope. As asked, question number one actually is the antipattern. If you need the factory 100 times, you inject it 100 times. It's barely any extra code when minified, and makes it very clear where the factory is used, and it makes it easier (and more obvious) to test those controllers with mocks, by having the required factories all listed in the function signature. Commented Nov 25, 2013 at 20:06
  • 5
    @chris - I agree and have since come to the same conclusion after using Angular for some time. If you articulate this in an answer I will select it at the best answer (if that's possible, haven't overridden an answer selection before). Otherwise I will probably just edit this post when I find the time. Commented Nov 28, 2013 at 1:57