0

I have some javascript and html files: User.js, index.html, Door.js

I want use any function in User.js file.

I call getUserInfo in user.Js from Door.js file in function doorApplicationLoginPage.service

Error: [$injector:unpr] Unknown provider: UserServiceProvider <- UserService <- PerioMainController

var doorApplicationLoginPage = angular.module("PerioLoginPage", []);

doorApplicationLoginPage.service('UserService', function () {

    this.getUserInfo = function () {
        alert("getUserInfo");
    }

    this.reLoginUser = function () {
        alert("reLoginUser");
    }

});


var doorApplication = angular.module("PerioDoorApplication", []);

doorApplication.controller("PerioMainController", function ($scope, $http, $sce, UserService) {


    UserService.getUserInfo();

});

Thank you.

0

3 Answers 3

1

You are injecting a service which is not referenced to your module.

See:

The UserService service is referenced in PerioLoginPage module The PerioMainController controller is referenced in PerioDoorApplication module.

You've got either to:

  1. reference the service in the same module as your controller.
  2. inject the module where service is referenced to the module where controller is referenced.
Sign up to request clarification or add additional context in comments.

2 Comments

I disagree, he could use a "dependency" from the other module
You're right. This might happen if he would inject the module where service is referenced to the module where controller is referenced. Thanks for the reply. I am editing right forward.
0

In this case I can see you have two modules Periodicloginpage and periodicdoorapplication. So two services are defined in two modules. So you have to put the Periodicloginpage as dependency of periodicdoorapplication.

var doorApplication = angular.module("PerioDoorApplication", ["PerioLoginPage"]);

Comments

0

As I said in my comments you have two things to do at least:

You need to return the functions you want to use from the service (UserService):

return{
    getUserInfo: getUserInfo, 
    reLoginUser: reLoginUser
};

Your module needs to reference the one the service is defined on:

angular.module('PerioDoorApplication', ['PerioLoginPage']);

And in your 'PerioMainController', a better way to write it would be:

doorApplication.controller('PerioMainController', ['$scope', '$http', '$sce', 'UserService', 
    function($scope, $http, $sce, UserService){
        [...]
    }
]);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.