None
None

Reputation: 5670

Error in injecting a controller into another controller in AngularJS

My code is like this

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'
]);

Everything looks okay to me, But this throws an error

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

Can any one point out what I am doing wrong?

Upvotes: 0

Views: 27

Answers (1)

deitch
deitch

Reputation: 14601

You are using the dependency declaration incorrectly. It needs to be wrapped in an array. Instead of:

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

Try:

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

And even that isn't correct, because the names of the dependencies and the args need to match. Try:

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

Upvotes: 2

Related Questions