Skip to content Skip to sidebar Skip to footer

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 pa

Solution 1:

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.

Solution 2:

You can always just use regular html. Updated your plnkr here:

http://plnkr.co/edit/v0BYhg3ojCgy7eiebRvR?p=preview

This just requires you know the path to the HTML file.

<!DOCTYPE html><htmlng-app="mydemo"><head><scriptdata-require="angular.js@1.5.5"data-semver="1.5.5"src="https://code.angularjs.org/1.5.5/angular.min.js"></script><scriptdata-require="angular-animate@1.5.5"data-semver="1.5.5"src="https://code.angularjs.org/1.5.5/angular-animate.js"></script><scriptdata-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><scriptsrc="script.js"></script><linkhref="http://netdna.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" /></head><body><divng-controller="myCtrl"><buttontype="button"class="btn btn-default"ng-click="open()">Show modal</button></div></body></html>

and your new myContent.html

<divclass="modal-header"><h3class="modal-title">I'm a modal!</h3></div><divclass="modal-body">
    Modal body content goes here...
  </div><divclass="modal-footer"><buttonclass="btn"type="button"ng-click="cancel()">Close</button></div>

Solution 3:

Here is one more solution using $templateCache.

A forked plunker is here: http://plnkr.co/edit/ukqmThpvi4aRGLZEiSSe?p=preview

JS - templates.js

var app = angular.module('mydemo');

app.run(function($templateCache){

  $templateCache.put('tplcache/myContent.htm',
    '<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>');

})

JS - modal config

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

HTML Scripts (include templates.js after main app)

<scriptsrc="script.js"></script><scriptsrc="templates.js"></script>

HTML body

<body><divng-controller="myCtrl"><buttontype="button"class="btn btn-default"ng-click="open()">Show modal</button></div></body>

Post a Comment for "How To Pass Html Markup In Ui Bootstrap Modal"