diff --git a/aio/content/examples/interpolation/src/app/app.component.1.ts b/aio/content/examples/interpolation/src/app/app.component.1.ts new file mode 100644 index 0000000000000..00708f4a1d2ab --- /dev/null +++ b/aio/content/examples/interpolation/src/app/app.component.1.ts @@ -0,0 +1,21 @@ +import { Component } from '@angular/core'; + + +// #docregion var-collision +@Component({ + template: ` +
+ +

Hello, {{customer}}

+ +
+ ` +}) +class AppComponent { + customers = [{value: 'Ebony'}, {value: 'Chiho'}]; + customer = 'Padma'; +} +// #enddocregion var-collision diff --git a/aio/content/examples/interpolation/src/app/app.component.css b/aio/content/examples/interpolation/src/app/app.component.css index e69de29bb2d1d..0a5e9c8845294 100644 --- a/aio/content/examples/interpolation/src/app/app.component.css +++ b/aio/content/examples/interpolation/src/app/app.component.css @@ -0,0 +1,8 @@ +div { + max-width: 600px; + margin: auto; +} + +img { + max-width: 200px; +} diff --git a/aio/content/guide/interpolation.md b/aio/content/guide/interpolation.md index 42919ff06dfee..267e9ac70f100 100644 --- a/aio/content/guide/interpolation.md +++ b/aio/content/guide/interpolation.md @@ -96,25 +96,33 @@ This next example features a template reference variable, `#customerInput`. +
+ +Template expressions cannot refer to anything in the global namespace, except `undefined`. +They can't refer to `window` or `document`. +Additionally, they can't call `console.log()` or `Math.max()` and they are restricted to referencing members of the expression context. + +
+ +### Preventing name collisions + The context against which an expression evaluates is the union of the template variables, the directive's context object—if it has one—and the component's members. -If you reference a name that belongs to more than one of these namespaces, Angular applies the following logic to determine the context: +If you reference a name that belongs to more than one of these namespaces, Angular applies the following logic to determine the context: 1. The template variable name. 1. A name in the directive's context. 1. The component's member names. -The previous example presents such a name collision. The component has a `customer` -property and the `*ngFor` defines a `customer` template variable. +To avoid variables shadowing variables in another context, keep variable names unique. +In the following example, the `AppComponent` template greets the `customer`, Padma. -
+An `ngFor` then lists each `customer` in the `customers` array. -The `customer` in `{{customer.name}}` refers to the template input variable, not the component's property. + -Template expressions cannot refer to anything in the global namespace, except `undefined`. -They can't refer to `window` or `document`. -Additionally, they can't call `console.log()` or `Math.max()` and they are restricted to referencing members of the expression context. - -
+The `customer` within the `ngFor` is in the context of an `` and so refers to the `customer` in the `customers` array, in this case Ebony and Chiho. +This list does not feature Padma because `customer` outside of the `ngFor` is in a different context. +Conversely, `customer` in the `

` doesn't include Ebony or Chiho because the context for this `customer` is the class and the class value for `customer` is Padma. ## Expression best practices