How to inject services into a provider using angular 1.3

In every piece of information I can find (including the angular documentation), the way to inject a service into the provider is with the method $get

:

var myApp = angular.module('myApp', []);

myApp.provider('helloWorld', function() {
    this.$get = function() {
        return {
            sayHello: function() {
                return "Hello, World!"
            }
        }
    };
});

function MyCtrl($scope, helloWorld) {    
    $scope.hellos = [helloWorld.sayHello()];
}

      

This works great in angular 1.2 and below: http://jsfiddle.net/1kjL3w13/

Switch to angular 1.3 and the function $get

will break completely. It seems that whatever was returned from the function $get

is no longer used to instantiate the provider and hence is now useless for fe service injection.

Same example as above but using angular 1.3 : http://jsfiddle.net/duefnz47/

This is exactly the behavior described in the angular documentation. So either the documentation is wrong or I have completely misunderstood it. I don't care if the method $get

still works or not, but I just need to reliably inject the services into my provider.

+3


source to share


1 answer


The problem is you are using a global controller which is invalid according to angular 1.3

So use

angular.module('myApp').controller('MyCtrl',function ($scope, helloWorld) {    
    $scope.hellos = [helloWorld.sayHello()];
});

      

Here's the fiddle updated



**

Official document on migration

** Hope it helps :)

+2


source







All Articles