2016-08-30 39 views
1

我想調用app.js文件中的控制器方法[getFacility()]。 但是當我用下面的代碼段,我得到了錯誤,如如何從角度調用app.js中的控制器方法

Error: [$injector:unpr] Unknown provider:

這裏我控制器文件和服務文件

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

     }; 

    } 
]); 

這裏我得到了我的控制檯。

"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

相關問題