当前位置: 移动技术网 > IT编程>脚本编程>AngularJs > AngularJS入门教程之AngularJS表达式

AngularJS入门教程之AngularJS表达式

2018年06月17日  | 移动技术网IT编程  | 我要评论

表达式用于应用程序数据绑定到html。表达式都写在双括号就像{{表达式}}。表达式中的行为跟ng-bind指令方式相同。 angularjs应用表达式是纯javascript表达式,并输出它们被使用的数据在那里。

angularjs表达式格式 : {{expression }}

angularjs表达式可以是字符串、数字、运算符和变量

数字运算{{1 + 5}}

字符串连接{{ 'abc' + 'bcd' }}

变量运算 {{ firstname + " " + lastname }}, {{ quantity * cost }}

对象{{ person.lastname }}

数组{{ points[2] }}

angularjs例子

1.angularjs数字

<div ng-app="" ng-init="quantity=2;cost=5">
<p>总价: {{ quantity * cost }}</p>
</div> 

上例输出:

总价:10

代码注释:

ng-init="quantity=2;cost=5" //相当于javascript里的var quantity=2,cost=5;
使用ng-bind可以实现相同的功能

<div ng-app="" ng-init="quantity=1;cost=5">
<p>总价: <span ng-bind="quantity * cost"></span></p>
//这里的ng-bind相当于指定了span的innerhtml
</div> 

2.angularjs字符串

<div ng-app="" ng-init="firstname='john';lastname='snow'">
<p>姓名: {{ firstname + " " + lastname }}</p>
</div> 

输出

姓名:jone snow

3. angularjs对象

<div ng-app="" ng-init="person={firstname:'john',lastname:'snow'}">
<p>姓为 {{ person.lastname }}</p>
</div> 

输出

姓为 snow

4.angularjs数组

<div ng-app="" ng-init="points=[1,15,19,2,40]">
<p>第三个值为 {{ points[2] }}</p>
</div>

输出

第三个值为 19

以上所述是小编给大家介绍的angularjs入门教程之angularjs表达式的相关介绍,希望对大家有所帮助!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网