官术网_书友最值得收藏!

Template elements

Template elements allow you to define regions within your HTML, which will not be rendered by the browser. You can then instantiate these document fragments with JavaScript and then place the resulting DOM within your document.

While the browser is actually parsing the template content, it only does so in order to validate the HTML. Any immediate actions that the parser would usually execute will not be taken. Within the content of template elements, images will not be loaded and scripts won't be executed. Only after a template is instantiated will the parser take the necessary actions, as follows:

<body> 
<template id="template">
<h1>This is a template!</h1>
</template>
</body>

This simple HTML example of a template element won't display the heading on your page. As the heading is inside a template element, we first need to instantiate the template and add the resulting DOM into our document:

var template = document.querySelector('#template'); 
var instance = document.importNode(template.content, true);
document.body.appendChild(instance);

Using these three lines of JavaScript, we can instantiate the template and append it into our document.

Template elements are used by Angular in order to instantiate dynamic parts of your user interface. This will be the case while conditionally rendering parts of your template using the ngIf directive, or by repeating a template using the ngFor directive.
主站蜘蛛池模板: 磴口县| 峡江县| 中山市| 惠东县| 新民市| 望江县| 大足县| 景东| 永济市| 大连市| 浦东新区| 伊川县| 始兴县| 清远市| 垫江县| 乐清市| 罗甸县| 凤翔县| 海晏县| 临泽县| 康马县| 略阳县| 上思县| 濉溪县| 乌兰浩特市| 江门市| 古交市| 紫金县| 山阴县| 甘德县| 渭南市| 赣州市| 开封市| 阿拉尔市| 叙永县| 满城县| 六枝特区| 定州市| 开封县| 东安县| 沾益县|