1

I need a help. I use Angularjs 1.6 and I want just simply to inject a service from diff file in a controller. Looks pretty easy, aha) Before I read this: AngularJS: Service in different file. But it didn't work in my case.

My code looks next:

app.js

angular.module('myApp', ['formService', 'formLogic'])

component.js

angular.module('formLogic',[])
.component('formLogic',{
    templateUrl: './templates/form-logic.html',
    controller: function ($scope, formService){

    }
});

service.js

angular.module('formService',[])
.service('FormService', function($http){

});

But I got this error: Error: $injector:unpr Unknown Provider

Thanks for your help!

1
  • A-ha) it seems I inject in a wrong module and I don't need upperCase for the service. Commented Aug 10, 2017 at 22:33

2 Answers 2

2
  • Don't give your modules the same name as your providers (don't name both your module and your component "formLogic").
  • Add your "formService" module as a dependency on your "formLogic" module.
  • You have called your service "FormService" and are trying to inject "formService" into your component controller. "formService" is not a service that you have defined, it's the name of your module.
Sign up to request clarification or add additional context in comments.

Comments

0

To inject component and services from different module. Can inject the module.

component.js

angular.module('anotherModule',[])
.component('formLogic',{
    templateUrl: './templates/form-logic.html',
    controller: function ($scope, formService){

    }
});

service.js

angular.module('anotherModule')
.service('formService', function($http){

});

so no can use formService and formLogic from myApp module

like:

app.js

angular.module('myApp', ['anotherModule']) // injected `anotherModule` in `myApp` modele

mainController.js

angular.module('myApp').controller('MainCtrl', function($scope, formService) {
  // can access `formService` from here
});

OR

you can use same module like:

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

app.component('formLogic',{
   templateUrl: './templates/form-logic.html',
   controller: function ($scope, formService){
      // ...
   }
});

app.service('formService', function($http){
    //.....
 });

app.controller('MainCtrl', function($scope, formService) {
    // can access `formService` from here
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.