diff --git a/public/docs/_examples/template-syntax/ts/app/app.component.html b/public/docs/_examples/template-syntax/ts/app/app.component.html index d9c7aba1ce..5a567265fb 100644 --- a/public/docs/_examples/template-syntax/ts/app/app.component.html +++ b/public/docs/_examples/template-syntax/ts/app/app.component.html @@ -255,7 +255,10 @@
{{ }}
.,
- we know that it is a property of a parent component.
- When we see `[disabled]="isUnchanged"` or `(click)="onCancel()”`,
- we know we are referring to that component's `isUnchanged` property and `onCancel` method respectively.
+ When we see *title* wrapped in double-curly braces, {{title}}
,
+ we know that `title` is a property of the data-bound component.
+ When we see *isUnchanged* in `[disabled]="isUnchanged"`,
+ we know we are referring to that component's `isUnchanged` property.
The component itself is usually the expression *context* in which case
the template expression usually references that component.
+
The expression context may include an object other than the component.
-
- A [local template variable](#local-vars) is one such supplemental context object;
- we’ll discuss that option below.
-
- Another is the **`$event`** variable that contains information about an event raised on an element;
- we’ll talk about that when we consider [Event Bindings](#event-binding).
+ A [local template variable](#local-vars) is one such alternative context object.
+
+
+
+ ### Expression Guidelines
+ Template expressions can make or break an application.
+ Please follow these guidelines unless you have an exceptionally good reason to break them
+ in specific circumstances that you thoroughly understand.
+
+ #### No visible side-effects
+
+ A template expression should have ***no visible side-effects***.
+ We're not allowed to change any application state other than the value of the
+ target property.
+
+ This rule is essential to Angular's "unidirectional data flow" policy.
+ We should never worry that reading a component value might change some other displayed value.
+ The view should be stable throughout a single rendering pass.
+
+ #### Finish fast
+ Angular executes template expressions more often than we might think.
+ Expressions should finish quickly or the user experience may drag, especially on slower devices.
+
+ #### Keep them simple
+ Although we can write quite complex template expressions, we strongly discourage that practice.
+ Most readers frown on JavaScript in the HTML.
+ A property name or method call should be the norm.
+ An occasional Boolean negation (`!`) is OK.
+ Otherwise, confine application and business logic to the component itself where it will be easier to develop and test.
+
+ #### Idempotent Expressions
+ An [idempotent](https://en.wikipedia.org/wiki/Idempotence) expression is ideal because
+ it is free of side-effects and improves Angular's change detection performance.
+
+ In Angular terms, an idempotent expression always returns *exactly the same thing* until
+ one of its dependent values changes.
+
+ Dependent values should not change during a single turn of the JavaScript virtual machine.
+ If an idempotent expression returns a string or a number, it returns the same string or number
+ when called twice in a row. If the expression returns an object (including a `Date` or `Array`),
+ it returns the same object *reference* when called twice in a row.
+
+
+.l-main-section
+:marked
+ ## Template Statements
+
+ A template **statement** responds to an ***event*** raised by a binding target
+ such as an element, component, or directive.
+
+ We’ll see template statements in [Event Bindings](#event-binding),
+ appearing in quotes to the right of the (=) symbol as in `(event)="statement"`.
+
+ A template statement *has a side-effect*.
+ It's how we update application state from user input.
+ There would be no point to responding to an event otherwise.
+.l-sub-section
+ :marked
+ Responding to events is the other side of Angular's "unidirectional data flow".
+ We're free to change anything, anywhere, during this turn of the JavaScript virtual machine.
+:marked
+ Angular template statements are also written in a language that looks like JavaScript.
+ The template statement parser is different than the template expression parser and
+ specifically supports both assignment (=) and chaining expressions with semicolons (;) and commas (,).
+
+ However, certain JavaScript syntax is not allowed:
+ * the `new` operator
+ * increment and decrement operators, `++` and `--`
+ * bit-wise operators, `|` and `&`
+ * the [template expression operators](#expression-operators)
+
+ As with expressions, we cannot refer to anything in the global namespace.
+ We can’t refer to `window` or `document`. We can’t call `console.log` or `Math.max`.
+
+ We are restricted to referencing members of the statement context.
+ The **statement context** is typically the **component instance** to which we are binding an event.
+
+ The *onSave* in `(click)="onSave()"` is sure to be a method of the data-bound component instance.
+
+ The statement context may include an object other than the component.
+ A [local template variable](#local-vars) is one such alternative context object.
+ We'll frequently see the reserved `$event` symbol in event binding statements,
+ representing the "message" or "payload" of the raised event.
.l-sub-section
:marked
- Although we can write quite complex template expressions, we strongly discourage that practice. Most readers frown on JavaScript in the HTML. A property name or method call should be the norm. An occasional Boolean negation (`!`) is OK. Otherwise, confine application and business logic to the component itself where it will be easier to develop and test.
+ Although we can write quite complex template statements, we strongly discourage that practice.
+ Most readers frown on JavaScript in the HTML.
+ A method call or simple property assignment should be the norm.
:marked
- Now that we have a feel for template expressions, we’re ready to learn about the varieties of data binding syntax beyond Interpolation.
-
+ Now that we have a feel for template expressions and statements,
+ we’re ready to learn about the varieties of data binding syntax beyond Interpolation.
.l-main-section
:marked
## Binding syntax overview
- Data binding is a mechanism for coordinating what users see with application data values. While we could push values to and pull values from HTML,
+ Data binding is a mechanism for coordinating what users see with application data values.
+ While we could push values to and pull values from HTML,
the application is easier to write, read, and maintain if we turn these chores over to a binding framework.
- We simply declare bindings between the HTML and the data properties and let the framework do the work.
+ We simply declare bindings between binding sources and target HTML elements and let the framework do the work.
Angular provides many kinds of data binding and we’ll discuss each of them in this chapter.
First we'll take a high level view of Angular data binding and its syntax.
- We can group all bindings into three categories by the direction in which data flows. Each category has its distinctive syntax:
+ We can group all bindings into three categories by the direction in which data flows.
+ Each category has its distinctive syntax:
table
tr
th Data Direction
@@ -165,8 +256,8 @@ table
td One way