2

MY code is like this

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

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

         $scope.openModal();
    });
});

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


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

As you can see I am trying to call a function $scope.openModal(); inside another controller, And obviously it throws an error

TypeError: undefined is not a function

at line

   $scope.openModal();

Is there any way to overcome this issue?

4 Answers 4

2

Since controllers are not injectable, I would not try. Try leveraging shared services instead.

Sign up to request clarification or add additional context in comments.

4 Comments

Well they may be injectable through the $controller service... but why would you do that? I would argue its not good design.
I don't completely understand your answer can you please elaborate more?
You cannot inject 'ReadOnlyController' into another controllers function. Angular does not allow that. There is a good reason, it was not designed to allow that.
Isn't there any other solution for this issue?
1

You can inject a controller into another controller using angular controller service $controller

angular.module('RateRequestApp.controllers', []).controller('ReadOnlyController', ['$controller','$scope', function($scope, rateRequestService,$controller) {
    var modalDCtrl = $scope.$new();
    $controller('ModalDemoCtrl',{$scope : modalDCtrl });
    $scope.rateData = [];
    rateRequestService.getData().success(function(response) {
        $scope.rateData = response;
    }).error(function (data, status, headers, config) {
        modalDCtrl.openModal();
    });
}]);

However you also need to attach the method to the controller instance instead of $scope.

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

Referencing : How do I inject a controller into another controller in AngularJS

4 Comments

Your code gives me an error Error: [ng:areq] Argument 'ReadOnlyController' is not a function, got string
Edited. Perhaps because I forgot '[' for dependency injection. Basically use the same code setup you had but -inject $controller service into your 'ReadOnlyController' - Define the other controller using $controller('ModalDemoCtrl',{$scope : modalDCtrl }); - change "$scope" to "this" in 'ModalDemoCtrl'
Now this throws an error TypeError: undefined is not a function at line modalDCtrl.openModal();
Try to create an isolated scope by passing true in the $new() method - $new(true).
0

Define your controllers inside the app and then inject them as required. You haven't mentioned the implementation of your model instance controller. So I assume you have forgotten to implement it.

angular.module('RateRequestApp.controllers', []).controller('ReadOnlyController', function($scope, rateRequestService) { 
                // ReadOnlyController's logic
}));

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

/**
 *  This might be missing
 */
var ModalInstanceCtrl = function ($scope, $modal) {
            // ModalInstanceController's logic
};

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

Hope above snippet would be helpful :)

Comments

0

$controller will helpfull, try this link https://docs.angularjs.org/guide/controller you will get good idea.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.