表达式有两种写法,一种是{{ expression }}
一种是ng-bind="expression"
可以包含常量,操作符和变量。
不过在使用中我们发现:{{ expression }}主要用于直接展示:
<div ng-app="">
ng-app必须要,这个是为什么??
<p>My
first expression: {{ 5 + 5 }}</p>
</div>
对于ng-bind="expression",都说了是bind了,那肯定就是要绑定到某个上面,就是在HTML标签内部绑定啊。
<div ng-app="" ng-init="quantity=1;cost=5">
init不常用,学完Controller会有更好的方式
<p>Total
in dollar: <span ng-bind="quantity * cost"></span></p>
</div>
之外还支持数字,上面有
Strings
<div ng-app="" ng-init="firstName='John';lastName='Doe'">
<p>The
name is {{ firstName + " " + lastName }}</p>
</div>
Arrays
<div ng-app="" ng-init="points=[1,15,19,2,40]">
<p>The
third result is {{ points[2] }}</p>
</div>
Object
<div ng-app="" ng-init="person={firstName:'John',lastName:'Doe'}">
<p>The
name is {{ person.lastName }}</p>
</div>
AngularJS表达式和JavaScript表达式区别
相同:都可以包含常量,操作符,变量。
不同:JavaScript不能在HTML标签内写,Angular不支持条件,循环,和异常,但是支持过滤器,而js不
支持过滤器