6

I am trying to make a modal which can be instantiated from multiple places in the app. from the example given here: Angular directives for Bootstrap the modal controller is in the same file as the controller who instantiates the modal. I want to separate the modal controller from the "app" controller.

index.html:

<!DOCTYPE html>
<html ng-app="modalTest">

  <head>
    <link rel="stylesheet" type="text/css" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
  </head>

  <body ng-controller="modalTestCtrl">
    <button ng-click="openModal()">Modal</button>

    <script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.20/angular.min.js"></script>
    <script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.10.0/ui-bootstrap.min.js"></script>
    <script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.10.0/ui-bootstrap-tpls.min.js"></script>
    <script type="text/javascript" src="modalTest.js"></script>
  </body>

</html>

The controller:

var app = angular.module('modalTest', ['ui.bootstrap']);

app.controller('modalTestCtrl', ['$scope', '$modal', function($scope, $modal) {
    $scope.openModal = function() {
        var modalInstance = $modal.open({
            templateUrl: 'modal.html',
            controller: 'modalCtrl',
            size: 'sm'

        });
    }
 }]);
// I want this in another JavaScript file... 
app.controller('modalCtrl', ['$scope', function($scope) {
    $scope.hello = 'Works';
}]);

modal.html:

<div ng-controller="modalCtrl">
    <div class="modal-header">
        <h3 class="modal-title">I'm a modal!</h3>
    </div>
    <div class="modal-body">
        <h1>{{hello}}</h1>
    </div>
    <div class="modal-footer">
        <button class="btn btn-primary" ng-click="ok()">OK</button>
        <button class="btn btn-warning" ng-click="cancel()">Cancel</button>
    </div>
</div>

Is there any way to put the modalCtrl in another JavaScript file and somehow inject the controller (modalCtrl) into the modalTestCtrl ?

BR

2
  • I'm guessing you probably want to use a directive for this unless there is a specific reason you have to use a controller Commented Aug 31, 2014 at 22:29
  • Lets say that the modal is a login modal and when the session time outs the modal has to be shown. Then the modal controller has to have all the login logic Commented Aug 31, 2014 at 22:33

1 Answer 1

9

Of course, you can do it. First of all you should use a function declaration.

// modalCtrl.js
// This file has to load before modalTestCtrl controller
function modalCtrl ($scope) {
   $scope.hello = 'Works';
};

Then, change the modalTestCtrl like:

app.controller('modalTestCtrl', ['$scope', '$modal', function($scope, $modal) {
$scope.openModal = function() {
    var modalInstance = $modal.open({
        templateUrl: 'modal.html',
        controller: modalCtrl, //This must be a referance, not a string
        size: 'sm'

    });
}
}]);

With the changes above, you code has to work.

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

1 Comment

mini warning: modalCtrl function must be directly declared in modalCtrl.js, not in self executing function :D (it bugged me for 15 mins until I noticed that :D)

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.