AngularJS Expressions

AngularJS usesexpressionto bind data to HTML.

AngularJS Expressions

AngularJS expressions can be written within double curly braces:{{ expression }}.

AngularJS expressions can also be written within directives:ng-bind="expression".

AngularJS will parse the expression and return the result at the location where the expression is written.

AngularJS Expressionsare very similar to JavaScript expressions: They can contain literals, operators, and variables.

Example {{ 5 + 5 }} or {{ firstName + " " + lastName }}

Example

<!DOCTYPE html>
<html>
<script src="https://cdn.staticfile.net/angular.js/1.6.9/angular.min.js"></script>
<body>
<div ng-app="">
  <p>My first expression: {{ 5 + 5 }}</p>
</div>
</body>
</html>

Try it yourself

If you remove ng-app Directives, HTML will display expressions as is without parsing them:

Example

<!DOCTYPE html>
<html>
<script src="https://cdn.staticfile.net/angular.js/1.6.9/angular.min.js"></script>
<body>
<div>
  <p>My first expression: {{ 5 + 5 }}</p>
</div>
</body>
</html>

Try it yourself

You can write expressions at any location you like, and AngularJS will simply parse the expression and return the result.

Example: Let AngularJS change the value of a CSS property.

Change the color of the following input box by changing its value:

Light blue

Example

<div ng-app="" ng-init="myCol='lightblue'">
<input style="background-color:{{myCol}}" ng-model="myCol">
</div>

Try it yourself

AngularJS Numbers

AngularJS numbers are similar to JavaScript numbers:

Example

<div ng-app="" ng-init="quantity=1;cost=5">
<p>Total amount (USD): {{ quantity * cost }}</p>
</div>

Try it yourself

Using ng-bind Same example:

Example

<div ng-app="" ng-init="quantity=1;cost=5">
<p>Total amount (USD): <span ng-bind="quantity * cost"></span></p>
</div>

Try it yourself

Using ng-init is not common. You will learn better methods for initializing data in the chapter about controllers.

AngularJS Strings

AngularJS strings are similar to JavaScript strings:

Example

<div ng-app="" ng-init="firstName='Bill';lastName='Gates'">
<p>Name is {{ firstName + " " + lastName }}</p>
</div>

Try it yourself

Same example using ng-bind:

Example

<div ng-app="" ng-init="firstName='Bill';lastName='Gates'">
<p>Name is <span ng-bind="firstName + ' ' + lastName"></span></p>
</div>

Try it yourself

AngularJS Objects

AngularJS objects are similar to JavaScript objects:

Example

<div ng-app="" ng-init="person={firstName:'Bill',lastName:'Gates'}">
<p>Name is {{ person.lastName }}</p>
</div>

Try it yourself

Same example using ng-bind:

Example

<div ng-app="" ng-init="person={firstName:'Bill',lastName:'Gates'}">
<p>Name is <span ng-bind="person.lastName"></span></p>
</div>

Try it yourself

AngularJS Arrays

AngularJS arrays are similar to JavaScript arrays:

Example

<div ng-app="" ng-init="points=[1,15,19,2,40]">
<p>The third result is {{ points[2] }}</p>
</div>

Try it yourself

Same example using ng-bind:

Example

<div ng-app="" ng-init="points=[1,15,19,2,40]">
<p>The third result is <span ng-bind="points[2]"></span></p>
</div>

Try it yourself

AngularJS expressions vs. JavaScript expressions

Similar to JavaScript expressions, AngularJS expressions can contain literals, operators, and variables.

Unlike JavaScript expressions, AngularJS expressions can be written inside HTML.

AngularJS expressions do not support conditional statements, loops, and exceptions, while JavaScript expressions do.

AngularJS expressions support filters, while JavaScript expressions do not.

Learn about JavaScript in our JavaScript Tutorial.