Angular expressions are JavaScript-like code snippets that are usually placed in bindings such as
{{ expression }}
.
Expressions are evaluated against a
scope object.
You can do simple math (+, -, /, *, %), make comparisons (==, !=, >, <, >=, <=), perform boolean logic (&&, ||, !) and bitwise operations (\^, &, |). You can call functions you expose on
$scope
in your controller and you can reference arrays and object notation ([ ], { }, .).
Using numbers
<div ng-app ng-init="cost=120;quantity=5">
<p>Total cost of Products : ${{cost * quantity}}</p>
{{5+6}}
</div>
Using strings
<div ng-app ng-init="firstName='Abraham';lastName='Lincoln'">
<p>Hello {{ firstName + " " + lastName }}!</p>
<p>Hello {{ 'Abraham' + ' ' + 'Lincoln' }}!</p>
</div>
Using object
<div ng-app ng-init="employee={Name:'Peter',Designation:'Engineer'}">
<p>The name is {{ employee.Name }}</p>
</div>
Using array
<div ng-app ng-init="results=[1,15,19,2,40]">
<p>The fourth result is {{ results[3] }}</p>
</div>
All of these are valid examples of expressions:
snippet
<div ng-controller='SomeController'>
<div>{{recompute() / 10}}</div>
<ul ng-repeat='thing in things'>
<li ng-class='{highlight: $index % 4 >= threshold($index)}'>
{{otherFunction($index)}}
</li>
</ul>
{{1+2}} {{a+b}} {{user.name}} {{items[index]}}
</div>