1

I am a little confused about the following Angular.js concepts:

  • factory
  • service
  • dependency injection

Can anyone brief me on each one with a simple example or explanation? Any help would be appreciated.

Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265
divakar
  • 1,379
  • 6
  • 15
  • 31
  • hope helps: http://stackoverflow.com/questions/15666048/service-vs-provider-vs-factory, http://stackoverflow.com/questions/13762228/confused-about-service-vs-factory/13763886#13763886 – Marian Ban Sep 20 '14 at 17:17

1 Answers1

0

These concepts are part of JavaScript core.

RegExp is a Factory:

console.log(RegExp("[0-9]") );
console.log(RegExp("[a-z]") );
console.log(RegExp("[A-Z]") );
console.log(RegExp("[0-9a-zA-Z]") );

Math is a Service:

console.log(Math.PI);
console.log(Math.round(Math.PI));
console.log(Number(Math.random() * 1000).toFixed());
console.log(Number(Math.random() * 10).toPrecision(2));
console.log(Math.floor(Math.random() * 20) + 1);

call and apply are Dependency Injection:

"use strict";

var foo = {
    min: function min(array) {
        return Math.min.apply(Math, array);
    },

    max: function max(array) {
        return Math.max.apply(Math, array);
    }
};

var bar = foo.min([1,2,3]);
var baz = foo.max([1,2,3]);

console.log("bar: " + bar);
console.log("baz: " + baz);

References

Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265