1

app.js 파일에서 컨트롤러 메서드 [getFacility()]를 호출하려고합니다. 내가각도에서 app.js에서 컨트롤러 메서드를 호출하는 방법

여기

Error: [$injector:unpr] Unknown provider:

내 컨트롤러 파일과

서비스 파일과 같은 오류를 가지고 아래의 코드 세그먼트를 사용하는 경우 하지만

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

.... 
... 

facilityControlelr.js을 app.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); 
       } 
      ); 

     }; 

    } 
]); 

여기 내 콘솔에 무엇이 있습니다.

"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 [email protected]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 [email protected]https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js:4550:39 [email protected]https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js:4574:58 [email protected]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 [email protected]https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js:9492:9 [email protected]https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js:8978:11 [email protected]https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js:8226:13 [email protected]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 [email protected]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 [email protected]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 [email protected]https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js:11266:36 [email protected]https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js:11464:7 [email protected]https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js:11405:9

+0

왜 컨트롤러에 상태를 주입하지 않습니까? –

+0

plnkr을 만들 수 있습니까? – krutkowski86

+0

설명해 주시겠습니까?이 코드를 통해 무엇을 성취하려고합니까? –

답변

1

가 다른 컨트롤러에 컨트롤러를 삽입하려면 $controller 서비스를 사용 :

몇 가지 문서를 참조하십시오.

.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); 
     } 
} 

facilityController manipuates $scope 때문에 $scope로컬로 주입 될 필요가있다.

자세한 내용은 AngularJS $controller Service API Reference을 참조하십시오.

-1

다른 컨트롤러에는 컨트롤러를 삽입 할 수 없습니다. 공장/서비스가 필요합니다. , https://docs.angularjs.org/guide/services

+0

그렇다면 어떻게'$ stateProvider.state' 내부에서 컨트롤러 메소드를 호출 할 수 있습니까? – Sadun89

+0

수 없습니다. 컨트롤러간에 데이터/로직을 공유하려면 서비스가 필요합니다. – vjarysta

+0

은 좋지 않습니다. 권장하지는 않지만. 컨트롤러를 다른 컨트롤러에 삽입 할 수 있습니다. – undefined