I'm trying to use a template
tag inside an Angular component.
Following the MDN example I created a similar stackblitz example.
In the original example, the template
tag has no children but it's content has children. On my example it's the other way around. The template has the children and content is empty (open the console and click the button to see it).
As a result, trying to manipulate the cloned template content fails.
What am I doing wrong?
app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
name = 'Angular';
click() {
const template: HTMLTemplateElement = document.querySelector('#t');
console.log(`template.childElementCount: ${template.childElementCount}`);
console.log(`template.content.childElementCount: ${template.content.childElementCount}`);
const content = document.importNode(template.content, true);
console.log(content.querySelector('.c2'));
}
}
app.component.html
<button (click)="click()">test</button>
<template id="t">
<div class="c1">
<div class="c2">{{ name }}</div>
</div>
</template>
Console result