填写这份《一分钟调查》,帮我们(开发组)做得更好!去填写Home

模板表达式运算符

Template expression operators

已标记为归档
Marked for archiving

为确保你拥有最佳的体验,本主题已标记为存档,直到我们确定其清楚地传达了最准确的信息为止。

To ensure that you have the best experience possible, this topic is marked for archiving until we determine that it clearly conveys the most accurate information possible.

同时,下列主题可能会有所帮助:分层注入器

In the meantime, this topic might be helpful: Hierarchical injectors.

如果你认为不应将此内容存档,请提交 GitHub 问题

If you think this content should not be archived, please file a GitHub issue.

Angular 模板表达语言采用了 JavaScript 语法的子集,并为特定情况添加了一些特殊的运算符。

The Angular template expression language employs a subset of JavaScript syntax supplemented with a few special operators for specific scenarios.

有关包含本指南中代码片段的有效示例,请参见现场演练 / 下载范例

See the现场演练 / 下载范例for a working example containing the code snippets in this guide.

非空断言运算符( !

The non-null assertion operator ( ! )

使用 TypeScript 的 --strictNullChecks 标志时,可以防止类型检查器使用 Angular 的非空断言运算符 !

When you use TypeScript's --strictNullChecks flag, you can prevent the type checker from throwing an error with Angular's non-null assertion operator, !.

Angular 非空断言运算符使 TypeScript 类型检查器暂停对特定属性表达式的 nullundefined 的严格检查。

The Angular non-null assertion operator causes the TypeScript type checker to suspend strict null and undefined checks for a specific property expression.

例如,你可以断言 item 也是已定义的。

For example, you can assert that item properties are also defined.

src/app/app.component.html
      
      <!-- Assert color is defined, even if according to the `Item` type it could be undefined. -->
<p>The item's color is: {{item.color!.toUpperCase()}}</p>
    

通常,你要确保任何属性绑定都不为 nullundefined 。但是,在某些情况下,这种状态是可以接受的。对于这些情况,可以使用 Angular 的非空断言运算符来防止 TypeScript 报告某个属性为 nullundefined

Often, you want to make sure that any property bindings aren't null or undefined. However, there are situations in which such states are acceptable. For those situations, you can use Angular's non-null assertion operator to prevent TypeScript from reporting that a property is null or undefined.

非空断言运算符 ! 是可选的,除非你要启用严格的空检查。

The non-null assertion operator, !, is optional unless you turn on strict null checks.

有关更多信息,请参见 TypeScript 的严格空检查

For more information, see TypeScript's strict null checking.

$any() 类型转换函数

The $any() type cast function

有时,绑定表达式会在 AOT 编译期间触发类型错误,并且不可能或很难完全指定类型。要使此错误静音,可以使用 $any() 强制转换函数把表达式强制转换为 any 类型,如下例所示:

Sometimes a binding expression triggers a type error during AOT compilation and it is not possible or difficult to fully specify the type. To silence the error, you can use the $any() cast function to cast the expression to the any type as in the following example:

src/app/app.component.html
      
      <p>The item's undeclared best by date is: {{$any(item).bestByDate}}</p>
    

使用 $any() 可以防止 TypeScript 报告 bestByDate 不是 item 对象成员的错误。

Using $any() prevents TypeScript from reporting that bestByDate is not a member of the item object.

$any() 强制转换函数也可以与 this 一起使用,以允许访问组件的未声明成员。

The $any() cast function also works with this to allow access to undeclared members of the component.

src/app/app.component.html
      
      <p>The item's undeclared best by date is: {{$any(this).bestByDate}}</p>
    

$any() 强制转换函数可在绑定表达式中任何进行方法调用的地方使用。

The $any() cast function works anywhere in a binding expression where a method call is valid.