Skip to content

Commit 9e8d31d

Browse files
committed
fix(compiler): clone templates before compiling them
This is needed as the compiler changes templates during compilation and we are caching templates in the `TemplateLoader`. Closes angular#1058
1 parent f75a50c commit 9e8d31d

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

modules/angular2/src/render/dom/compiler/template_loader.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ export class TemplateLoader {
3939
StringMapWrapper.set(this._htmlCache, url, promise);
4040
}
4141

42-
return promise;
42+
// We need to clone the result as others might change it
43+
// (e.g. the compiler).
44+
return promise.then( (tplElement) => DOM.clone(tplElement) );
4345
}
4446

4547
throw new BaseException('View should have either the url or template property set');

modules/angular2/test/render/dom/compiler/template_loader_spec.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,19 @@ export function main() {
4545
xhr.flush();
4646
}));
4747

48-
it('should cache template loaded through XHR', inject([AsyncTestCompleter], (async) => {
48+
it('should cache template loaded through XHR but clone it as the compiler might change it', inject([AsyncTestCompleter], (async) => {
4949
var firstEl;
50+
// we have only one xhr.expect, so there can only be one xhr call!
5051
xhr.expect('base/foo', 'xhr template');
5152
var template = new ViewDefinition({absUrl: 'base/foo'});
5253
loader.load(template)
5354
.then((el) => {
55+
expect(DOM.content(el)).toHaveText('xhr template');
5456
firstEl = el;
5557
return loader.load(template);
5658
})
5759
.then((el) =>{
58-
expect(el).toBe(firstEl);
60+
expect(el).not.toBe(firstEl);
5961
expect(DOM.content(el)).toHaveText('xhr template');
6062
async.done();
6163
});

0 commit comments

Comments
 (0)