From a51c990af48f21ccf351cf96e8f7763fa419247a Mon Sep 17 00:00:00 2001 From: frederikprijck Date: Sun, 4 Feb 2018 10:20:46 +0100 Subject: [PATCH] fix($templateRequest): always return the template that is stored in the cache Previously, `$templateRequest` returned the raw `$http` response data on the first request for a template and then the value from the cache for subsequent requests. If the value is transformed when being added to the cache (by decorating `$templateCache.put`) the return value of `$templateRequest` would be inconsistent depending upon when the request is made. This commit ensures the cached value is returned instead of the raw `$http` response data, thus allowing the `$templateCache` service to be decorated. Closes #16225 --- src/ng/templateRequest.js | 3 +-- test/ng/templateRequestSpec.js | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/ng/templateRequest.js b/src/ng/templateRequest.js index 7b3b04261e56..ff699d6cd0ef 100644 --- a/src/ng/templateRequest.js +++ b/src/ng/templateRequest.js @@ -99,8 +99,7 @@ function $TemplateRequestProvider() { handleRequestFn.totalPendingRequests--; }) .then(function(response) { - $templateCache.put(tpl, response.data); - return response.data; + return $templateCache.put(tpl, response.data); }, handleError); function handleError(resp) { diff --git a/test/ng/templateRequestSpec.js b/test/ng/templateRequestSpec.js index cb9c1c6f6ce8..3ca323613103 100644 --- a/test/ng/templateRequestSpec.js +++ b/test/ng/templateRequestSpec.js @@ -114,6 +114,24 @@ describe('$templateRequest', function() { expect($templateCache.get('tpl.html')).toBe('matias'); })); + it('should return the cached value on the first request', + inject(function($rootScope, $templateRequest, $templateCache, $httpBackend) { + + $httpBackend.expectGET('tpl.html').respond('matias'); + spyOn($templateCache, 'put').and.returnValue('_matias'); + + var content = []; + function tplRequestCb(html) { + content.push(html); + } + + $templateRequest('tpl.html').then(tplRequestCb); + $rootScope.$digest(); + $httpBackend.flush(); + + expect(content[0]).toBe('_matias'); + })); + it('should call `$exceptionHandler` on request error', function() { module(function($exceptionHandlerProvider) { $exceptionHandlerProvider.mode('log');