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

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.
主站蜘蛛池模板: 丹寨县| 高台县| 南安市| 旌德县| 察哈| 永川市| 绥宁县| 定南县| 永寿县| 辉县市| 安多县| 云和县| 陆川县| 徐汇区| 阆中市| 镇雄县| 岳普湖县| 鱼台县| 西畴县| 灵丘县| 蕲春县| 海城市| 安宁市| 佛坪县| 湖州市| 名山县| 彩票| 盘锦市| 尚志市| 北京市| 潍坊市| 固始县| 钦州市| 江北区| 泗洪县| 榆社县| 清涧县| 泰顺县| 平遥县| 湄潭县| 汨罗市|