|
3 | 3 | var $compileMinErr = minErr('$compile');
|
4 | 4 |
|
5 | 5 | /**
|
6 |
| - * @ngdoc service |
7 |
| - * @name $templateRequest |
8 |
| - * |
| 6 | + * @ngdoc provider |
| 7 | + * @name $templateRequestProvider |
9 | 8 | * @description
|
10 |
| - * The `$templateRequest` service runs security checks then downloads the provided template using |
11 |
| - * `$http` and, upon success, stores the contents inside of `$templateCache`. If the HTTP request |
12 |
| - * fails or the response data of the HTTP request is empty, a `$compile` error will be thrown (the |
13 |
| - * exception can be thwarted by setting the 2nd parameter of the function to true). Note that the |
14 |
| - * contents of `$templateCache` are trusted, so the call to `$sce.getTrustedUrl(tpl)` is omitted |
15 |
| - * when `tpl` is of type string and `$templateCache` has the matching entry. |
16 |
| - * |
17 |
| - * @param {string|TrustedResourceUrl} tpl The HTTP request template URL |
18 |
| - * @param {boolean=} ignoreRequestError Whether or not to ignore the exception when the request fails or the template is empty |
19 |
| - * |
20 |
| - * @return {Promise} a promise for the HTTP response data of the given URL. |
21 |
| - * |
22 |
| - * @property {number} totalPendingRequests total amount of pending template requests being downloaded. |
| 9 | + * Used to configure the Accept header that is sent to the server when requesting a template. |
23 | 10 | */
|
24 | 11 | function $TemplateRequestProvider() {
|
| 12 | + |
| 13 | + var acceptHeaders; |
| 14 | + |
| 15 | + /** |
| 16 | + * @ngdoc method |
| 17 | + * @name $templateRequestProvider#acceptHeaders |
| 18 | + * @description |
| 19 | + * The accept header to be sent to the server as part of the request. |
| 20 | + * If this value is undefined then the default {@link $http} Accept header |
| 21 | + * will be used. |
| 22 | + * |
| 23 | + * @param {string=} value new value for the Accept header. |
| 24 | + * @returns {string|self} Returns the Accept header value when used as getter and self if used as setter. |
| 25 | + */ |
| 26 | + this.acceptHeaders = function(val) { |
| 27 | + if (val) { |
| 28 | + acceptHeaders = val; |
| 29 | + return this; |
| 30 | + } |
| 31 | + return acceptHeaders; |
| 32 | + }; |
| 33 | + |
| 34 | + /** |
| 35 | + * @ngdoc service |
| 36 | + * @name $templateRequest |
| 37 | + * |
| 38 | + * @description |
| 39 | + * The `$templateRequest` service runs security checks then downloads the provided template using |
| 40 | + * `$http` and, upon success, stores the contents inside of `$templateCache`. If the HTTP request |
| 41 | + * fails or the response data of the HTTP request is empty, a `$compile` error will be thrown (the |
| 42 | + * exception can be thwarted by setting the 2nd parameter of the function to true). Note that the |
| 43 | + * contents of `$templateCache` are trusted, so the call to `$sce.getTrustedUrl(tpl)` is omitted |
| 44 | + * when `tpl` is of type string and `$templateCache` has the matching entry. |
| 45 | + * |
| 46 | + * @param {string|TrustedResourceUrl} tpl The HTTP request template URL |
| 47 | + * @param {boolean=} ignoreRequestError Whether or not to ignore the exception when the request fails or the template is empty |
| 48 | + * |
| 49 | + * @return {Promise} a promise for the HTTP response data of the given URL. |
| 50 | + * |
| 51 | + * @property {number} totalPendingRequests total amount of pending template requests being downloaded. |
| 52 | + */ |
25 | 53 | this.$get = ['$templateCache', '$http', '$q', '$sce', function($templateCache, $http, $q, $sce) {
|
| 54 | + |
26 | 55 | function handleRequestFn(tpl, ignoreRequestError) {
|
27 | 56 | handleRequestFn.totalPendingRequests++;
|
28 | 57 |
|
@@ -50,6 +79,10 @@ function $TemplateRequestProvider() {
|
50 | 79 | transformResponse: transformResponse
|
51 | 80 | };
|
52 | 81 |
|
| 82 | + if (acceptHeaders) { |
| 83 | + httpOptions.headers = { 'Accept': acceptHeaders }; |
| 84 | + } |
| 85 | + |
53 | 86 | return $http.get(tpl, httpOptions)
|
54 | 87 | ['finally'](function() {
|
55 | 88 | handleRequestFn.totalPendingRequests--;
|
|
0 commit comments