2019-12-02 12:47:19 -08:00
# Template syntax
2017-03-27 16:08:53 +01:00
2017-07-30 00:03:22 +08:00
# 模板语法
2017-03-30 20:04:18 +01:00
2020-04-28 16:26:58 -04:00
In Angular, a *template* is a chunk of HTML.
Within a template, you can use special syntax to leverage many of Angular's features.
2017-02-22 18:09:39 +00:00
2020-11-28 12:50:51 +08:00
在 Angular 中,*模板*就是一块 HTML。在模板中, 你可以通过一种特殊语法来使用 Angular 的许多功能。
2017-02-22 18:09:39 +00:00
2020-04-28 16:26:58 -04:00
## Prerequisites
2017-07-23 11:51:25 +08:00
2020-11-28 12:50:51 +08:00
## 先决条件
2017-02-22 18:09:39 +00:00
2020-04-28 16:26:58 -04:00
Before learning template syntax, you should be familiar with the following:
2018-03-03 21:06:01 +08:00
2020-11-28 12:50:51 +08:00
在学习模板语法之前,你应该熟悉下列内容:
2017-02-22 18:09:39 +00:00
2020-04-28 16:26:58 -04:00
* [Angular concepts ](guide/architecture )
2017-08-04 08:47:51 +08:00
2020-11-28 12:50:51 +08:00
[Angular 的概念 ](guide/architecture )
2017-02-22 18:09:39 +00:00
2020-04-28 16:26:58 -04:00
* JavaScript
* HTML
* CSS
2017-02-22 18:09:39 +00:00
2018-03-03 21:06:01 +08:00
2020-04-28 16:26:58 -04:00
<!-- Do we still need the following section? It seems more relevant to those coming from AngularJS, which is now 7 versions ago. -->
<!-- You may be familiar with the component/template duality from your experience with model - view - controller (MVC) or model - view - viewmodel (MVVM).
In Angular, the component plays the part of the controller/viewmodel, and the template represents the view. -->
2017-02-22 18:09:39 +00:00
2020-04-28 16:26:58 -04:00
< hr / >
2017-07-23 11:51:25 +08:00
2020-04-28 16:26:58 -04:00
Each Angular template in your app is a section of HTML that you can include as a part of the page that the browser displays.
An Angular HTML template renders a view, or user interface, in the browser, just like regular HTML, but with a lot more functionality.
2017-07-23 11:51:25 +08:00
2020-11-28 12:50:51 +08:00
应用程序中的每个 Angular 模板都是一块 HTML, 你可以将其包含在浏览器显示的页面中。 Angular 中的 HTML 模板与常规 HTML 一样,可以在浏览器中渲染视图或用户界面,但功能要多得多。
2017-03-27 16:08:53 +01:00
2020-04-28 16:26:58 -04:00
When you generate an Angular app with the Angular CLI, the `app.component.html` file is the default template containing placeholder HTML.
2018-03-12 13:04:24 +08:00
2020-11-28 12:50:51 +08:00
使用 Angular CLI 生成 Angular 应用时, `app.component.html` 文件是一个包含占位符 HTML 的默认模板。
2020-01-11 18:10:12 +08:00
2020-04-28 16:26:58 -04:00
The template syntax guides show you how you can control the UX/UI by coordinating data between the class and the template.
2018-12-03 13:42:53 -05:00
2020-11-28 12:50:51 +08:00
本模板语法指南向你展示了如何通过协调类和模板之间的数据来控制 UX/UI。
2020-01-11 18:10:12 +08:00
2020-04-28 16:26:58 -04:00
< div class = "is-helpful alert" >
2020-01-11 18:10:12 +08:00
2020-04-28 16:26:58 -04:00
Most of the Template Syntax guides have dedicated working example apps that demonstrate the individual topic of each guide.
To see all of them working together in one app, see the comprehensive < live-example title = "Template Syntax Live Code" > < / live-example > .
2020-01-11 18:10:12 +08:00
2020-11-28 12:50:51 +08:00
大多数模板语法指南都有专门的可工作的范例应用,这些应用程序演示了每个指南中的各个主题。要查看把所有应用合在一起时的工作情况,请参阅综合< live-example title = "Template Syntax Live Code" > < / live-example > 。
2017-03-27 16:08:53 +01:00
2017-04-10 16:51:13 +01:00
< / div >
2017-02-22 18:09:39 +00:00
2020-04-28 16:26:58 -04:00
## Empower your HTML
2017-02-22 18:09:39 +00:00
2020-11-28 12:50:51 +08:00
## 增强你的 HTML
2017-04-01 01:57:13 +02:00
2020-04-28 16:26:58 -04:00
With special Angular syntax in your templates, you can extend the HTML vocabulary of your apps.
For example, Angular helps you get and set DOM (Document Object Model) values dynamically with features such as built-in template functions, variables, event listening, and data binding.
2017-03-11 13:44:25 +00:00
2020-11-28 12:50:51 +08:00
通过在模板中使用 Angular 的特有语法,你可以扩展应用程序的 HTML 词汇表。例如, Angular 可以通过内置的模板函数、变量、事件监听和数据绑定等功能来帮助你动态获取和设置 DOM( 文档对象模型) 中的值。
2017-03-11 13:44:25 +00:00
2020-04-28 16:26:58 -04:00
Almost all HTML syntax is valid template syntax.
However, because an Angular template is part of an overall webpage, and not the entire page, you don't need to include elements such as `<html>` , `<body>` , or `<base>` .
You can focus exclusively on the part of the page you are developing.
2017-03-11 13:44:25 +00:00
2020-11-28 12:50:51 +08:00
几乎所有的 HTML 语法都是有效的模板语法。但是,由于 Angular 模板只是整个网页的一部分,而不是整个网页,因此你不需要包含诸如 `<html>` , `<body>` 或 `<base>` 元素。这样你可以专注于正在开发的那部分页面。
2017-03-11 13:44:25 +00:00
2020-04-28 16:26:58 -04:00
< div class = "alert is-important" >
2017-02-22 18:09:39 +00:00
2020-04-28 16:26:58 -04:00
To eliminate the risk of script injection attacks, Angular does not support the `<script>` element in templates.
Angular ignores the `<script>` tag and outputs a warning to the browser console.
For more information, see the [Security ](guide/security ) page.
2017-02-22 18:09:39 +00:00
2020-11-28 12:50:51 +08:00
为了消除脚本注入攻击的风险, Angular 不支持模板中使用 `<script>` 元素。Angular 会忽略 `<script>` 标记,并向浏览器控制台输出一条警告。欲知详情,参阅[“安全性” ](guide/security )页面。
2017-02-22 18:09:39 +00:00
2020-01-11 18:10:12 +08:00
< / div >
2017-02-22 18:09:39 +00:00
2020-04-28 16:26:58 -04:00
< hr / >
2017-12-12 12:51:38 -08:00
2020-04-28 16:26:58 -04:00
## More on template syntax
2017-12-12 12:51:38 -08:00
2020-11-28 12:50:51 +08:00
## 关于模板语法的更多信息
2018-03-12 13:04:24 +08:00
2020-04-28 16:26:58 -04:00
You may also be interested in the following:
2017-12-12 12:51:38 -08:00
2020-11-28 12:50:51 +08:00
你可能也对以下内容感兴趣:
2018-03-12 13:04:24 +08:00
2020-04-28 16:26:58 -04:00
* [Interpolation ](guide/interpolation )— learn how to use interpolation and expressions in HTML.
2017-12-12 12:51:38 -08:00
2020-11-28 12:50:51 +08:00
[插值 ](guide/interpolation ) - 学习如何在 HTML 中使用插值和表达式。
2017-12-12 12:51:38 -08:00
2020-04-28 16:26:58 -04:00
* [Template statements ](guide/template-statements )— respond to events in your templates.
2018-03-12 13:04:24 +08:00
2020-11-28 12:50:51 +08:00
[模板语句 ](guide/template-statements ) - 响应模板中的事件。
2017-12-12 12:51:38 -08:00
2020-04-28 16:26:58 -04:00
* [Binding syntax ](guide/binding-syntax )— use binding to coordinate values in your app.
2018-03-12 13:04:24 +08:00
2020-11-28 12:50:51 +08:00
[绑定语法 ](guide/binding-syntax ) - 使用绑定来协调应用程序中的值。
2017-12-12 12:51:38 -08:00
2020-04-28 16:26:58 -04:00
* [Property binding ](guide/property-binding )— set properties of target elements or directive `@Input()` decorators.
2017-07-23 11:51:25 +08:00
2020-11-28 12:50:51 +08:00
[属性绑定 ](guide/property-binding ) - 设置目标元素的属性或指令中带 `@Input()` 装饰器的属性。
2020-01-11 18:10:12 +08:00
2020-04-28 16:26:58 -04:00
* [Attribute, class, and style bindings ](guide/attribute-binding )— set the value of attributes, classes, and styles.
2019-06-26 11:46:04 +05:30
2020-11-28 12:50:51 +08:00
[属性( Attribute) , 类和样式绑定 ](guide/attribute-binding ) - 设置属性、类和样式的值。
2020-01-11 18:10:12 +08:00
2020-04-28 16:26:58 -04:00
* [Event binding ](guide/event-binding )— listen for events and your HTML.
2019-06-26 11:46:04 +05:30
2020-11-28 12:50:51 +08:00
[事件绑定 ](guide/event-binding ) - 监听事件和 HTML。
2020-01-11 18:10:12 +08:00
2020-04-28 16:26:58 -04:00
* [Two-way binding ](guide/two-way-binding )— share data between a class and its template.
2019-06-26 11:46:04 +05:30
2020-11-28 12:50:51 +08:00
[双向绑定 ](guide/two-way-binding ) - 在类及其模板之间共享数据。
2020-01-11 18:10:12 +08:00
2020-04-28 16:26:58 -04:00
* [Built-in directives ](guide/built-in-directives )— listen to and modify the behavior and layout of HTML.
2019-06-26 11:46:04 +05:30
2020-11-28 12:50:51 +08:00
[内置指令 ](guide/built-in-directives ) - 监听和修改 HTML 的行为和布局。
2020-01-11 18:10:12 +08:00
2020-04-28 16:26:58 -04:00
* [Template reference variables ](guide/template-reference-variables )— use special variables to reference a DOM element within a template.
2019-06-26 11:46:04 +05:30
2020-11-28 12:50:51 +08:00
[模板引用变量 ](guide/template-reference-variables ) - 使用特殊变量来引用模板中的 DOM 元素。
2020-01-11 18:10:12 +08:00
2020-04-28 16:26:58 -04:00
* [Inputs and Outputs ](guide/inputs-outputs )— share data between the parent context and child directives or components
2019-06-26 11:46:04 +05:30
2020-11-28 12:50:51 +08:00
[输入和输出 ](guide/inputs-outputs ) - 在父级上下文和子指令或组件之间共享数据
2019-06-26 11:46:04 +05:30
2020-04-28 16:26:58 -04:00
* [Template expression operators ](guide/template-expression-operators )— learn about the pipe operator, `|` , and protect against `null` or `undefined` values in your HTML.
2020-01-11 18:10:12 +08:00
2020-11-28 12:50:51 +08:00
[模板表达式运算符 ](guide/template-expression-operators ) - 了解管道运算符 `|` ,以及如何防范空值 `null` 或 `undefined` 。
2019-06-26 11:46:04 +05:30
2020-04-28 16:26:58 -04:00
* [SVG in templates ](guide/svg-in-templates )— dynamically generate interactive graphics.
2020-01-11 18:10:12 +08:00
2020-11-28 12:50:51 +08:00
[模板中的 SVG ](guide/svg-in-templates ) - 动态生成交互式图形。
2020-01-11 18:10:12 +08:00