我想从app.js文件中调用控制器方法[getFacility()]。 但是当我使用下面的代码段时,我得到了错误,如
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);
}
);
};
}
]);
这是我在控制台中得到的内容。
“错误:[$ injector:unpr]未知提供者:facilityControllerProvider < - facilityController http://errors.angularjs.org/1.5.0/ $注射器/ unpr?P0 = facilityControllerProvider%20%3C-NaNacilityController minErr /< @ https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js:68:12 createInjector / providerCache $注射器< @ 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 实例@ https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js:4616:18 $ ControllerProvider /这$ 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 升@ 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 /这$ 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 /这$ gethttps://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js:16820:16 $ RootScopeProvider /这$ gethttps://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js:16636:15 $ RootScopeProvider /这$ gethttps://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js:16928:13 做@ 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
答案 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
操纵$scope
,$scope
需要作为本地注入。
有关详细信息,请参阅AngularJS $controller Service API Reference。
答案 1 :(得分:-1)
您无法在另一个控制器中注入控制器。你需要工厂/服务。