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

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.
主站蜘蛛池模板: 岑溪市| 巨野县| 兴安盟| 永寿县| 历史| 雅安市| 正阳县| 安远县| 阿瓦提县| 怀来县| 江油市| 集安市| 东安县| 文安县| 阿城市| 克山县| 建阳市| 六枝特区| 台南县| 安达市| 遂川县| 湘阴县| 遂溪县| 迭部县| 玛多县| 英超| 财经| 运城市| 尤溪县| 赣榆县| 百色市| 平远县| 嘉善县| 尚志市| 喀什市| 西安市| 革吉县| 紫金县| 偏关县| 麟游县| 常熟市|