0

I'm learning angular1 and I came across services and factories. I called the .factory() method as shown below by passing in a string representing the desired service name. I was later able to reference a variable identical to the string name but I never actually created the variable. Does anyone know why this is the case? I imagine it's something to do with dependency injection but I can't understand the mechanism by which angular/js achieves this auto-variable creation.

app.factory('myData', function() {
    return {...}
}

app.controller('MyController',
    function MyController($scope, myData) {
        ...
    }
);

1 Answers1

0

Services and Factories are singleton in Angular and are instantiated through the dependency injection pattern, their references are resolved runtime by the core of angular.

This means that the developer is not in charge to manage directly the references, when the controller is executed the references are injected and you can use them accordingly.

app.controller('MyController',
    function MyController($scope, myData) {
        ...
    }
);

doc on services here: https://docs.angularjs.org/guide/services

Dependency injection here: https://en.wikipedia.org/wiki/Dependency_injection

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Karim
  • 8,454
  • 3
  • 25
  • 33
  • That's what I expected but angular is itself written in javascript. Surely this means that under the hood there must be something on the lines of: var [x] = – StretchBJJ101 Oct 13 '16 at 10:41
  • var [x] = ..., where [x] is a string. I didn't think this was possible in javascript. I'm not aware of any feature that allows you to create variable names from strings – StretchBJJ101 Oct 13 '16 at 10:44
  • I don't understand your concerns. yes, somewhere behind the curtain Angular does something like that, var myData = myDataFunction() in this case the function that represents the factory is called once and then whatever is returned is cached. Everytime you inject myData in a controller that value is passed as parameter by Angular. Instead in case of a Service Angular calls a NEW on the function, this is the only difference, var myService = new myServiceFunction(), and even in this case the instance is cached infact is a singleton (one instance for the entire app), like having a global variable – Karim Oct 13 '16 at 11:19
  • It's not so much a concern as it is a point of interest. Obviously I can just accept that angular does this. I'm just curious how it can declare a variable name (reference identifier) directly from a string that I specified. Perhaps I'm missing some concept (e.g. scope) but I assume if angular is able to achieve this then it must be possible in standard javascript. For example: `var x = [y]//input from user (e.g. 'hello')` `var [y] = 'world' //var hello = 'world'` – StretchBJJ101 Oct 13 '16 at 11:48
  • it's a sort of reflection, here, at the bottom of the page the user Jack Allan posted a function that extrapolates the names of the parameters of a function passed as input, i think this answer your question.http://stackoverflow.com/questions/1007981/how-to-get-function-parameter-names-values-dynamically-from-javascript – Karim Oct 13 '16 at 12:51