Eike Cochu
Eike Cochu

Reputation: 3429

Angular UI Bootstrap: not loading templates

I am using Angular UI Bootstrap v1.0.3 with templates included (I chose include templates, I can see the *tpls modules in the source code), yet when I open a modal like this (from within app.run(...)):

var type = "sometype";
$uibModal.open({
    templateUrl: "/partials/" + type + "-dialog.html",
});

I get an error:

angular.min.js:93 GET http://.../uib/template/modal/backdrop.html?sessionid=363192 404 (Not Found)(anonymous function) @ angular.min.js:93r @ angular.min.js:89g @ angular.min.js:86(anonymous function) @ angular.min.js:119r.$eval @ angular.min.js:133r.$digest @ angular.min.js:130r.$apply @ angular.min.js:134g @ angular.min.js:87T @ angular.min.js:92w.onload @ angular.min.js:93 angular.min.js:107

Error: [$compile:tpload] http://errors.angularjs.org/1.4.8/$compile/tpload?p0=uib%2Ftemplate%2Fmodal%2Fbackdrop.html&p1=404&p2=Not%20Found at Error (native)

I tried adding the template manually to my code and added the following code to the top of my app.js:

angular.module("template/modal/backdrop.html", []).run(["$templateCache", function($templateCache) {
     $templateCache.put("template/modal/backdrop.html", "<div class=\"modal-backdrop\"></div>");
}]);

still the same error

Upvotes: 3

Views: 2479

Answers (1)

Eike Cochu
Eike Cochu

Reputation: 3429

I found the error.

The problem was that the template was requested using query parameters that I added with a request interceptor. I added an exception to that interceptor matching the template url prefix and now it works.

var noSessionIdUrls = [
    "uib/template",
    "template"
];

app.config(['$httpProvider', function($httpProvider) {
    $httpProvider.interceptors.push( function ($q, $injector, $rootScope) { 
        return {
            request: function(config) {
                for(var i = 0; i < noSessionIdUrls.length; i++) {
                    if(config.url.startsWith(noSessionIdUrls[i])) {
                        console.log("request interceptor: omitting session id");
                        return config;
                    }
                }

                config.url = config.url + '?sessionid=' + window.sessionid;
                return config;
            }
        };
    });
}]);

Upvotes: 5

Related Questions