{ "id": "guide/interpolation", "title": "Text interpolation", "contents": "\n\n\n
\n mode_edit\n
\n\n\n
\n

Text interpolationlink

\n

Text interpolation allows you to incorporate dynamic string values into your HTML templates.\nWith interpolation, you can dynamically change what appears in an application view, such as displaying a custom greeting that includes the user's name.

\n
\n

See the for all of the syntax and code snippets in this guide.

\n
\n

Displaying values with interpolationlink

\n

Interpolation refers to embedding expressions into marked up text.\nBy default, interpolation uses the double curly braces {{ and }} as delimiters.

\n

To illustrate how interpolation works, consider an Angular component that contains a currentCustomer variable:

\n\ncurrentCustomer = 'Maria';\n\n\n

You can use interpolation to display the value of this variable in the corresponding component template:

\n\n<h3>Current customer: {{ currentCustomer }}</h3>\n\n\n

Angular replaces currentCustomer with the string value of the corresponding component property.\nIn this case, the value is Maria.

\n

In the following example, Angular evaluates the title and itemImageUrl properties to display some title text and an image.

\n\n<p>{{title}}</p>\n<div><img src=\"{{itemImageUrl}}\"></div>\n\n\n

Template expressionslink

\n

A template expression produces a value and appears within double curly braces, {{ }}.\nAngular resolves the expression and assigns it to a property of a binding target.\nThe target could be an HTML element, a component, or a directive.

\n

Resolving expressions with interpolationlink

\n

More generally, the text between the braces is a template expression that Angular first evaluates and then converts to a string.\nThe following interpolation illustrates the point by adding two numbers:

\n\n<!-- \"The sum of 1 + 1 is 2\" -->\n<p>The sum of 1 + 1 is {{1 + 1}}.</p>\n\n\n

Expressions can also invoke methods of the host component such as getVal() in the following example:

\n\n<!-- \"The sum of 1 + 1 is not 4\" -->\n<p>The sum of 1 + 1 is not {{1 + 1 + getVal()}}.</p>\n\n\n

With interpolation, Angular performs the following tasks:

\n
    \n
  1. Evaluates all expressions in double curly braces.
  2. \n
  3. Converts the expression results to strings.
  4. \n
  5. Links the results to any adjacent literal strings.
  6. \n
  7. Assigns the composite to an element or directive property.
  8. \n
\n
\n

You can configure the interpolation delimiter with the interpolation option in the @Component() metadata.

\n
\n

Syntaxlink

\n

Template expressions are similar to JavaScript.\nMany JavaScript expressions are legal template expressions, with the following exceptions.

\n

You can't use JavaScript expressions that have or promote side effects, including:

\n\n

Other notable differences from JavaScript syntax include:

\n\n

Expression contextlink

\n

Interpolated expressions have a context—a particular part of the application to which the expression belongs.\nTypically, this context is the component instance.

\n

In the following snippet, the expression recommended and the expression itemImageUrl2 refer to properties of the AppComponent.

\n\n<h4>{{recommended}}</h4>\n<img [src]=\"itemImageUrl2\">\n\n\n

An expression can also refer to properties of the template's context such as a template input variable or a template reference variable.

\n

The following example uses a template input variable of customer.

\n\n<ul>\n <li *ngFor=\"let customer of customers\">{{customer.name}}</li>\n</ul>\n\n\n

This next example features a template reference variable, #customerInput.

\n\n<label>Type something:\n <input #customerInput>{{customerInput.value}}\n</label>\n\n\n
\n

Template expressions cannot refer to anything in the global namespace, except undefined.\nThey can't refer to window or document.\nAdditionally, they can't call console.log() or Math.max() and they are restricted to referencing members of the expression context.

\n
\n

Preventing name collisionslink

\n

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.\nIf you reference a name that belongs to more than one of these namespaces, Angular applies the following logic to determine the context:

\n
    \n
  1. The template variable name.
  2. \n
  3. A name in the directive's context.
  4. \n
  5. The component's member names.
  6. \n
\n

To avoid variables shadowing variables in another context, keep variable names unique.\nIn the following example, the AppComponent template greets the customer, Padma.

\n

An ngFor then lists each customer in the customers array.

\n\n@Component({\n template: `\n <div>\n <!-- Hello, Padma -->\n <h1>Hello, {{customer}}</h1>\n <ul>\n <!-- Ebony and Chiho in a list-->\n <li *ngFor=\"let customer of customers\">{{ customer.value }}</li>\n </ul>\n </div>\n `\n})\nclass AppComponent {\n customers = [{value: 'Ebony'}, {value: 'Chiho'}];\n customer = 'Padma';\n}\n\n\n

The customer within the ngFor is in the context of an <ng-template> and so refers to the customer in the customers array, in this case Ebony and Chiho.\nThis list does not feature Padma because customer outside of the ngFor is in a different context.\nConversely, customer in the <h1> doesn't include Ebony or Chiho because the context for this customer is the class and the class value for customer is Padma.

\n

Expression best practiceslink

\n

When using template expressions, follow these best practices:

\n\n\n \n
\n\n\n" }