将控制器注入AngularJS中的另一个控制器时出错

时间:2014-11-28 11:29:38

标签: javascript angularjs

我的代码就像这样

Controller.JS
    angular.module('RateRequestApp.controllers', []).controller('ReadOnlyController',  '$controller', function($scope, rateRequestService,$controller) {

    $controller('ModalDemoCtrl',{$scope : modalDCtrl });
    $scope.rateData = [];

    rateRequestService.getData().success(function(response) {
        $scope.rateData = response;
    }).error(function (data, status, headers, config) {

        modalDCtrl.openModal();
    });
});
angular.module('RateRequestApp.controllers').controller('ModalDemoCtrl', function ($scope, $modal, $log) {

    this.openModal = function (size) {

        var modalInstance = $modal.open({
            templateUrl: 'myModalContent.html',
            controller: 'ModalInstanceCtrl',
            size: size,

        });
    }; 
});
App.JS
angular.module('RateRequestApp', [
   'RateRequestApp.services',
   'RateRequestApp.controllers',
   'ui.bootstrap'
]);

一切看起来都没问题,但这会引发错误

Error: [ng:areq] Argument 'ReadOnlyController' is not a function, got string

任何人都可以指出我做错了吗?

1 个答案:

答案 0 :(得分:2)

您正在错误地使用依赖项声明。它需要包装在一个数组中。而不是:

.controller('ReadOnlyController',  '$controller', function($scope, rateRequestService,$controller) {
   // your stuff
});

尝试:

.controller('ReadOnlyController',  ['$controller', function($scope, rateRequestService,$controller) {
   // your stuff
}]);

即使这样也不正确,因为依赖项和args的名称需要匹配。尝试:

.controller('ReadOnlyController',  ['$scope','rateRequestService','$controller', function($scope, rateRequestService,$controller) {
   // your stuff
}]);