Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

I want to call the controller method [getFacility()] from the app.js file. but when I used the below code segment I got the error like

Error: [$injector:unpr] Unknown provider:

here my controller files and service files

app.js

...
...     
.state('facilityHome.editFacility', 
{
   url: '/editFacility/:facilityId',
   templateUrl: '/views/facility/newFacility.html',
   controller: 
        function($scope, $stateParams, facilityController) {
                facilityController.getFacility($stateParams.facilityId);
         }
}

....
...

facilityControlelr.js

app.controller('facilityController',
['$scope', '$rootScope','$location','$filter', 'facilityService',
    function ($scope, $rootScope,$location,$filter, facilityService){

        /* Assign Object */
        var facilityScope = this;

        /* Initialize DTO Object */
        facilityScope.facilityDTO={
            id:null,
            version:null,
            facilityName:"",
            description:""
        };


        /* Initialize Object Array */
        facilityScope.facilityList = [];


        facilityScope.getFacility=function(id){
            facilityService.fetchFacility(id)
                .then(
                function(successRespond) {
                    $scope.facilityDTO = successRespond;
                },
                function(errResponse){
                    console.error('Error while fetching');
                    console.error(errResponse);
                }
            );

        };

    }
]);

Here What I got in my console.

"Error: [$injector:unpr] Unknown provider: facilityControllerProvider <- facilityController http://errors.angularjs.org/1.5.0/$injector/unpr?p0=facilityControllerProvider%20%3C-NaNacilityController minErr/<@https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js:68:12 createInjector/providerCache.$injector<@https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js:4397:19 getService@https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js:4550:39 createInjector/protoInstanceInjector<@https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js:4402:28 getService@https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js:4550:39 injectionArgs@https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js:4574:58 instantiate@https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js:4616:18 $ControllerProvider/this.$gethttps://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js:9870:18 z/<.compile/<@https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.3.1/angular-ui-router.min.js:7:23873 invokeLinkFn@https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js:9492:9 nodeLinkFn@https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js:8978:11 compositeLinkFn@https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js:8226:13 publicLinkFn@https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js:8106:30 compilationGenerator/<@https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js:8447:20 l@https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.3.1/angular-ui-router.min.js:7:23072 y/l.compile/https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.3.1/angular-ui-router.min.js:7:23492 $RootScopeProvider/this.$gethttps://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js:17143:15 v/y.transitionTo/y.transition<@https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.3.1/angular-ui-router.min.js:7:18793 processQueue@https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js:15552:28 scheduleProcessQueue/<@https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js:15568:27 $RootScopeProvider/this.$gethttps://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js:16820:16 $RootScopeProvider/this.$gethttps://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js:16636:15 $RootScopeProvider/this.$gethttps://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js:16928:13 done@https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js:11266:36 completeRequest@https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js:11464:7 requestLoaded@https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js:11405:9

share|improve this question
    
Why not inject the state in the controller instead? – Marcus H 19 hours ago
    
Could you create plnkr? – krutkowski86 19 hours ago
    
Could you please explain, what are you trying to achieve by this code? – ciril sebastian 19 hours ago

You can't inject a Controller in another Controller. You need a Factory/Service.

See some documentation : https://docs.angularjs.org/guide/services

share|improve this answer
    
If so how I can call the controller method from the inside the $stateProvider.state ? – Sadun89 19 hours ago
    
You can't. You need a service, to share data/logic between controllers. – vjarysta 18 hours ago

To inject a controller into another controller, use the $controller service.

.state('facilityHome.editFacility', 
{
   url: '/editFacility/:facilityId',
   templateUrl: '/views/facility/newFacility.html',
   controller: 
       function($scope, $stateParams, $controller) {
           var facCtrl = $controller("facilityController", {'$scope': $scope});
           facCtrl.getFacility($stateParams.facilityId);
       }
}

Because the facilityController manipuates $scope, $scope needs to be injected as a local.

For more information,see AngularJS $controller Service API Reference.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.