user2851669
user2851669

Reputation:

calling one controller from another controller

I have 2 controllers and I am calling the second controller from the function of first controller

$scope.open = function () {
    var modal = $modal.open({
        templateUrl: 'views/view1.html',
        controller: 'controller2',
        // other parameters
    });
    /* some code */
}

Both the controllers are in the same folder. There are similar questions but I want to know if there is any way I can call the second controller without using a service.

Edit --

This is the error I am getting --

Error: [ng:areq] Argument 'controller2' is not a function, got undefined http://errors.angularjs.org/1.3.16/ng/areq?p0=controller2&p1=not%20a%20function%2C%20got%20undefined at REGEX_STRING_REGEXP (angular.js:63) at assertArg (angular.js:1590) at assertArgFn (angular.js:1600) at angular.js:8502 at resolveSuccess (ui-bootstrap-tpls.js:2377) at processQueue (angular.js:13292) at angular.js:13308 at Scope.$get.Scope.$eval (angular.js:14547) at Scope.$get.Scope.$digest (angular.js:14363) at Scope.$get.Scope.$apply (angular.js:14652)

Upvotes: 2

Views: 301

Answers (1)

Sifis
Sifis

Reputation: 653

You can normally use the controller in the modal open function but the controller2 must be in same module that controller1 belongs:

angular.module('myModule').controller('controller1', ['$scope', myModuleController1]); 
angular.module('myModule').controller('controller2', ['$scope', myModuleController2]);

or else you have to make your dependencies right in your app.js file if your controllers belong to separate modules myModule1, myModule2:

angular.module('myModule', [myModule2]);

Also, be sure that both controller is defined as files in index.html

    <script src="../controller1file.js"></script>
<script src="../controller2file.js"></script>

After all these steps you will be able to use the modal in the following way in the controller1 code:

function OpenModalsService($modal) {

var OpenModalsService = {};

var _openmodal = function () {

    var modalInstance = $modal.open({
        templateUrl: 'Modals.Views/view.html',
        controller: 'controller2',
        size: 50
    });

    modalInstance.result.then(function (var) {

       var = 'something';
    });
};

Hope it will help you!

Upvotes: 0

Related Questions