What is the type friendly injection?

All we need is an easy explanation of the problem, so here it is.

In the AngularJS documentation, there is an explanation of the differences between a factory, a service, a value, a constant and a provider .

At the end, we have a comparison table:
What is the type friendly injection?
One of the rows is “type friendly injection”. I could not understand what it is.

What does that mean? Additionally, what does it mean that, in order that a value will have this “type friendly injection”, is at the cost of “eager initialization by using new operator directly”?

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

In AngularJS, you can inject dependencies in multiple ways:

  • in the directive link function by position
  • in the directive definition by name
  • in the controller function by name
  • in the factory function by name
  • in the service function by type

Type friendly injection allows you to implicity invoke a constructor function by reference:

myApp.service('Pattern', ["Infinity", RegExp]);

rather than by explicity using the new keyword:

 ["Infinity", function(Infinity) 
  return new RegExp(Infinity);


function goInfinity(Infinity)
  return new RegExp(Infinity);

goInfinity.$inject = ["Infinity"];
myApp.factory('Pattern', goInfinity);

The Service recipe produces a service just like the Value or Factory recipes, but it does so by invoking a constructor with the new operator. The constructor can take zero or more arguments, which represent dependencies needed by the instance of this type.

Eager initialization means that a constant recipe must return a constructor in order to use the aforementioned syntax:

function RegExpConstant() 
  return new RegExp(Infinity);

myApp.constant('Pattern', RegExpConstant)

rather than returning a function, object, or literal value.

The nomenclature comes from Java:

A service is a well-known set of interfaces.
A service provider is a specific implementation of a service.
A factory is an object that returns an object reference to another object


Method 2

My interpretation of “type friendly injection”:

Factories and Providers inject whatever is returned by the factory function and $get function respectively, which could be of any type and could potentially change dynamically at runtime. Whereas Service, Constant and Value injections are of a fixed type that Angular is aware of as it is well defined during the definition of the recipe. Hence they are type friendly injections.

Method 3

Type here refers to:

  • primitives,
  • object literals,
  • functions, or
  • even an instance of a custom type

Factories and Providers are most flexible out of the Service, Constant, Value in terms of what they bind to the injector. Hence Angular can detect the type of object/primitive that is bound to Service, Constant, Value hence friendlier since we know the type upfront (design/build time).

Method 4

I just asked myself the same question when looking at the very same table. 🙂

My best guess interpretation of the values in that table row is that it actually means ‘whether you can find out what created the service instance from the service instance itself‘, i.e. ‘whether the service instance’s constructor property is set to a meaningful function‘.

Here’s a more detailed interpretation for each of the recipes:

  • factory: no, but can be yes if you construct the returned value by calling the new operator
  • service: yes – will reference the service recipe function
  • value/constant: yes, but only if you construct the value by calling the new operator
  • provider: no, but can be yes if you construct the provider.$get method’s return value by calling the new operator internally)

As it stands now, this part of the documentation seems unfinished and it is possible that the author had several ideas of what he wanted to show here but the final documentation managed to slip through the cracks & got published in some intermediate state.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply