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

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.
主站蜘蛛池模板: 蒲城县| 普格县| 敦煌市| 将乐县| 赤壁市| 安阳市| 杭锦旗| 宝鸡市| 汶上县| 顺义区| 菏泽市| 曲阳县| 马龙县| 高唐县| 航空| 永善县| 阿拉善左旗| 光山县| 都江堰市| 阿城市| 汤原县| 安吉县| 江华| 黄大仙区| 射洪县| 始兴县| 晴隆县| 年辖:市辖区| 麻栗坡县| 扶绥县| 东宁县| 江城| 奇台县| 阳泉市| 盐城市| 宁武县| 阜新市| 无极县| 深州市| 会泽县| 孟连|