简体   繁体   中英

Angular, injecting own service into provider

So I am working with a provider and injecting some of angular's $log and $location service in the $get to work with them, and this works fine. However when I want to inject my own service into the provider it does not seem to work.

Here's what I mean - I can inject the angular $log and $location like so (in the provider)

   this.$get = $get;
    $get.$inject = ['$log', '$location'];

    function $get( $log, $location) {
        return {
            setModule: function(name, cb) {

            },
            getModules: function() {

            }
        }
    }

But, I have a service below like

 .service("myService",function(){
 this.sayHello = function() {
    return "Hello, World!"
});

And try to inject it like

 $get.$inject = ['myService', '$log', '$location'];

    function $get(myService, $log, $location) {

When I call myService in the provider, it comes back undefined.

Is there any reason I can call the angular($) stuff and not my own to inject into the provider?

Thanks!

Edit : So after I inject it I try and use it inside the $get like

   $get.$inject = ['myService', '$log', '$location'];

    function $get(myService, $log, $location) {
      return {
          test: function(){
               console.log(myService);
           }
        }

returns undefined

1) pass $injector as a dependency in the controller ['$injector']

2) call $injector.get to instantiate the service by name

$scope.service = $injector.get(servicename);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM