Can you override specific templates in AngularUI Bootstrap?

The question:

I’m curious if there’s a way to override single, specific templates from the ui-bootstrap-tpls file. The vast majority of the default templates fit my needs, but there’s a couple specific ones I’d like to replace without going through the whole process of grabbing all the default templates and getting them wired up to the non-tpls version.

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

Yes, directives from are highly customizable and it is easy to override one of the templates (and still rely on the default ones for other directives).

It is enough to feed $templateCache, either feeding it directly (as done in the ui-bootstrap-tpls file) or – probably simpler – override a template using the <script> directive (doc).

A contrived example where I’m changing alert’s template to swap x for Close is shown below:

<!doctype html>
<html ng-app="plunker">
    <script src=""></script>
    <script src=""></script>
    <script src="example.js"></script>
    <link href="//" rel="nofollow noreferrer noopener" rel="stylesheet">

    <script id="template/alert/alert.html" type="text/ng-template">
      <div class='alert' ng-class='type && "alert-" + type'>
          <button ng-show='closeable' type='button' class='close' ng-click='close()'>Close</button>
          <div ng-transclude></div>

    <div ng-controller="AlertDemoCtrl">
      <alert ng-repeat="alert in alerts" type="alert.type" close="closeAlert($index)">                     
      <button class='btn' ng-click="addAlert()">Add Alert</button>

Live plunker:

Method 2

Using $provide.decorator

Using $provide to decorate the directive avoids the need to directly mess around with $templateCache.

Instead, create your external template html as you might normally, with whatever name you please, and then override the directive’s templateUrl to point at it.

angular.module('plunker', ['ui.bootstrap'])
  .config(['$provide', Decorate]);

  function Decorate($provide) {
    $provide.decorator('alertDirective', function($delegate) {
      var directive = $delegate[0];

      directive.templateUrl = "alertOverride.tpl.html";

      return $delegate;

Fork of pkozlowski.opensource‘s plunkr:

(Note that you must append the ‘Directive’ suffix to the directive name you intend to decorate. Above, we’re decorating UI Bootstrap’s alert directive, so we use the name alertDirective.)

As you may often want to do more than just override the templateUrl, this provides a good starting point from which to further extend the directive, for example by overriding/wrapping the link or compile function (for example).

Method 3

The answer from pkozlowski.opensource is really useful and helped me out a lot! I tweaked it in my condition to have a single file defining all of my angular template overrides and loaded the external JS to keep payload size down.

To do this, go to the bottom of the angular ui-bootstrap source js file (e.g. ui-bootstrap-tpls-0.6.0.js) and find the template you are interested in. Copy the entire block that defines the template and paste it into your overrides JS file.


angular.module("template/alert/alert.html", []).run(["$templateCache", function($templateCache) {
     "      <div class='alert' ng-class='type && "alert-" + type'>n" +
     "          <button ng-show='closeable' type='button' class='close' ng-click='close()'>Close</button>n" +
     "          <div ng-transclude></div>n" +
     "      </div>");

Then just include your overrides file after ui-bootstrap and you achieve the same result.

Forked version of pkozlowski.opensource‘s plunk

Method 4

You can use template-url="/app/.../_something.template.html" to override the current template for that directive.

(Works in Accordion Bootstrap at least.)

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment