ajay ajay - 6 months ago 60
AngularJS Question

How to pass HTML markup in UI bootstrap modal

I am using UI bootstrap to create modal dialog. It works fine if I use "templateUrl" and use "ng-template" to include the html template. But as I have multiple modal dialogs, my page is getting bigger and bigger after including all the html templates in the page using "ng-template".

Here's the code:
HTML


<head>
<script data-require="angular.js@1.5.5" data-semver="1.5.5" src="https://code.angularjs.org/1.5.5/angular.min.js"></script>
<script data-require="angular-animate@1.5.5" data-semver="1.5.5" src="https://code.angularjs.org/1.5.5/angular-animate.js"></script>
<script data-require="ui-bootstrap@1.3.2" data-semver="1.3.2" src="https://cdn.rawgit.com/angular-ui/bootstrap/gh-pages/ui-bootstrap-tpls-1.3.2.js"></script>
<script src="script.js"></script>
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
</head>

<body>
<div ng-controller="myCtrl">
<script type="text/ng-template" id="myContent.html">
<div class="modal-header">
<h3 class="modal-title">I'm a modal!</h3>
</div>
<div class="modal-body">
Modal body content goes here...
</div>
<div class="modal-footer">
<button class="btn" type="button" ng-click="cancel()">Close</button>
</div>
</script>

<button type="button" class="btn btn-default" ng-click="open()">Show modal</button>
</div>

</body>
</html>


Javascript:

angular.module('mydemo', ['ngAnimate', 'ui.bootstrap']);
angular.module('mydemo').controller('myCtrl', function ($scope, $uibModal, $log) {

$scope.open = function (size) {

var modalInstance = $uibModal.open({
templateUrl: 'myContent.html',
controller: 'ModalInstanceCtrl',
size: size
});
};
});

angular.module('mydemo').controller('ModalInstanceCtrl', function ($scope, $uibModalInstance) {
$scope.cancel = function () {
$uibModalInstance.dismiss('cancel');
};
});


Is there any way I can use HTML markup directly instead of using "templateUrl"?
Or atleast a way through which I can keep the html templates in a separate file instead of including it in the page using ng-template?
Here's the working example:
http://plnkr.co/edit/HqThAG79r22K2VGZSs2D?p=preview

Answer

Yep, you have a few options for indicating which content the modal will load.

  1. As @Wes answered and demonstrated in his plunker fork, you can create an external html file somewhere in your application, and as long as you specify the correct path to the file, the modal will work as expected.
  2. As you already have working the the plunker you linked to, you can put the template inside a script type="text/ng-template" tag and reference the value of its id attribute in your modal config.
  3. A final way is to inline the html in the modal config. Here's your plunker forked with the html directly added to the template property of the modal config as a string.

    var modalInstance = $uibModal.open({
      template: '<div class="modal-header">' +
                  '<h3 class="modal-title">I\'m a modal!</h3>' +
                '</div>' +
                '<div class="modal-body">' +
                  'Modal body content goes here...' +
                '</div>' +
                '<div class="modal-footer">' +
                  '<button class="btn" type="button" ng-click="cancel()">Close</button>' +
                '</div>',
      controller: 'ModalInstanceCtrl',
      size: size
    });
    

This method works just fine, but it can be a little cumbersome to write, read, and maintain, depending on the amount of markup in the template.

Comments