跳到正文
本页尚未翻译成中文,以下为英文原文。 查看英文版

Expression Syntax ​

Angular expressions are based on JavaScript, but differ in some key ways. This guide walks through the similarities and differences between Angular expressions and standard JavaScript.

Value literals ​

Angular supports a subset of literal values from JavaScript.

Supported value literals ​

Literal typeExample values
String'Hello', "World"
Booleantrue, false
Number123, 3.14
Object{name: 'Alice'}
Array['Onion', 'Cheese', 'Garlic']
nullnull
RegExp/\d+/
Template string`Hello ${name}`
Tagged template stringtag`Hello ${name}`

Unsupported value literals ​

Literal typeExample values
BigInt1n

Globals ​

Angular expressions support the following globals:

No other JavaScript globals are supported. Common JavaScript globals include Number, Boolean, NaN, Infinity, parseInt, and more.

Local variables ​

Angular automatically makes special local variables available for use in expressions in specific contexts. These special variables always start with the dollar sign character ($).

For example, @for blocks make several local variables corresponding to information about the loop, such as $index.

What operators are supported? ​

Supported operators ​

Angular supports the following operators from standard JavaScript.

OperatorExample(s)
Add / Concatenate1 + 2
Subtract52 - 3
Multiply41 * 6
Divide20 / 4
Remainder (Modulo)17 % 5
Exponentiation10 ** 3
Parenthesis9 * (8 + 4)
Conditional (Ternary)a > b ? true : false
And (Logical)&&
Or (Logical)||
Not (Logical)!
Nullish CoalescingpossiblyNullValue ?? 'default'
Comparison Operators<, <=, >, >=, ==, ===, !==, !=
Unary Negation-x
Unary Plus+y
Property Accessorperson['name']
typeoftypeof 42
voidvoid 1
in'model' in car
instanceofcar instanceof Automobile
Assignmenta = b
Incrementa++, ++a
Decrementa--, --a
Addition Assignmenta += b
Subtraction Assignmenta -= b
Multiplication Assignmenta *= b
Division Assignmenta /= b
Remainder Assignmenta %= b
Exponentiation Assignmenta **= b
Logical AND Assignmenta &&= b
Logical OR Assignmenta ||= b
Nullish Coalescing Assignmenta ??= b
Spread in object literals{...obj, foo: 'bar'}
Spread in array literals[...arr, 1, 2, 3]
Rest in function callsfn(...args)
Arrow functionitems.filter((i) => i.active)

NOTE

Assignment and increment/decrement operators are only valid in event listener statements. Using them in a binding or interpolation is a compile error, unless they are inside an arrow function.

Arrow functions must have an expression body. Pipes cannot be used inside an arrow function, but you can pass an arrow function as an argument to a pipe.

Angular expressions additionally also support the following non-standard operators:

OperatorExample(s)
Pipe{{ total | currency }}
Optional chaining*someObj.someProp?.nestedProp
Non-null assertion (TypeScript)someObj!.someProp

Safe navigation migration ​

Prior to Angular 22, the optional chaining operator (?.) returned null when the left-hand side is null or undefined, whereas standard JavaScript's ?. returns undefined. Since Angular 22, the optional chaining operator behavior in Angular expressions is aligned with the standard JavaScript behavior.

During the migration to v22, the ng update schematics adds a $safeNavigationMigration function to existing expressions to preserve the previous behavior.

html
{{ $safeNavigationMigration(foo?.bar) }}

$safeNavigationMigration is a temporary migration aid only. It instructs the compiler to compile the wrapped safe-navigation expression using the legacy null-returning semantics rather than the standard JavaScript ?. semantics. It is not a real function and cannot be called from TypeScript.

NOTE

Prefer migrating expressions to no longer rely on null vs undefined distinctions so that $safeNavigationMigration can be removed. This function may be removed in a future version of Angular.

Unsupported operators ​

OperatorExample(s)
All bitwise operators&, &=, ~, |=, ^=, etc.
Object destructuringconst { name } = person
Array destructuringconst [firstItem] = items
Comma operatorx = (x++, x)
newnew Car()

Lexical context for expressions ​

Angular expressions are evaluated within the context of the component class as well as any relevant template variables, locals, and globals.

When referring to component class members, this is always implied. However, if a template declares a template variables with the same name as a member, the variable shadows that member. You can unambiguously reference such a class member by explicitly using this.. This can be useful when creating an @let declaration that shadows a class member, e.g. for signal narrowing purposes.

Declarations ​

Generally speaking, declarations are not supported in Angular expressions. This includes, but is not limited to:

DeclarationsExample(s)
Variableslet label = 'abc', const item = 'apple'
Functionsfunction myCustomFunction() { }
Arrow functions with block bodies() => { }
Classesclass Rectangle { }

Event listener statements ​

Event handlers are statements rather than expressions. While they support all of the same syntax as Angular expressions, there are two key differences:

  1. Statements do support assignment operators (but not destructuring assignments)
  2. Statements do not support pipes

基于 MIT 许可证发布。