mirror of
https://github.com/zhigang1992/angular.js.git
synced 2026-06-13 08:30:36 +08:00
This handy service is designed to download and cache template contents and to throw an error when a template request fails. BREAKING CHANGE Angular will now throw a $compile minErr each a template fails to download for ngView, directives and ngMessage template requests. This changes the former behavior of silently ignoring failed HTTP requests--or when the template itself is empty. Please ensure that all directive, ngView and ngMessage code now properly addresses this scenario. NgInclude is uneffected from this change.
54 lines
1.7 KiB
JavaScript
54 lines
1.7 KiB
JavaScript
'use strict';
|
|
|
|
var $compileMinErr = minErr('$compile');
|
|
|
|
/**
|
|
* @ngdoc service
|
|
* @name $templateRequest
|
|
*
|
|
* @description
|
|
* The `$templateRequest` service downloads the provided template using `$http` and, upon success,
|
|
* stores the contents inside of `$templateCache`. If the HTTP request fails or the response data
|
|
* of the HTTP request is empty then a `$compile` error will be thrown (the exception can be thwarted
|
|
* by setting the 2nd parameter of the function to true).
|
|
*
|
|
* @param {string} tpl The HTTP request template URL
|
|
* @param {boolean=} ignoreRequestError Whether or not to ignore the exception when the request fails or the template is empty
|
|
*
|
|
* @return {Promise} the HTTP Promise for the given.
|
|
*
|
|
* @property {number} totalPendingRequests total amount of pending template requests being downloaded.
|
|
*/
|
|
function $TemplateRequestProvider() {
|
|
this.$get = ['$templateCache', '$http', '$q', function($templateCache, $http, $q) {
|
|
function handleRequestFn(tpl, ignoreRequestError) {
|
|
var self = handleRequestFn;
|
|
self.totalPendingRequests++;
|
|
|
|
return $http.get(tpl, { cache : $templateCache })
|
|
.then(function(response) {
|
|
var html = response.data;
|
|
if(!html || html.length === 0) {
|
|
return handleError();
|
|
}
|
|
|
|
self.totalPendingRequests--;
|
|
$templateCache.put(tpl, html);
|
|
return html;
|
|
}, handleError);
|
|
|
|
function handleError() {
|
|
self.totalPendingRequests--;
|
|
if (!ignoreRequestError) {
|
|
throw $compileMinErr('tpload', 'Failed to load template: {0}', tpl);
|
|
}
|
|
return $q.reject();
|
|
}
|
|
}
|
|
|
|
handleRequestFn.totalPendingRequests = 0;
|
|
|
|
return handleRequestFn;
|
|
}];
|
|
}
|