详解AngularJS中的表达式使用
- 作者: 如若情深不被辜负
- 来源: 51数据库
- 2021-07-11
表达式用于应用程序数据绑定到html。表达式都写在双括号就像{{表达式}}。表达式中的行为跟ng-bind指令方式相同。 angularjs应用表达式是纯javascript表达式,并输出它们被使用的数据在那里。
使用数字
<p>expense on books : {{cost * quantity}} rs</p>
使用字符串
<p>hello {{student.firstname + " " + student.lastname}}!</p>
使用对象
<p>roll no: {{student.rollno}}</p>
使用数组
<p>marks(math): {{marks[3]}}</p>
例子
下面的例子将展示上述所有表达式。
testangularjs.html 文件代码如下:
<html>
<title>angularjs expressions</title>
<body>
<h1>sample application</h1>
<div ng-app="" ng-init="quantity=1;cost=30; student={firstname:'mahesh',lastname:'parashar',rollno:101};marks=[80,90,75,73,60]">
<p>hello {{student.firstname + " " + student.lastname}}!</p>
<p>expense on books : {{cost * quantity}} rs</p>
<p>roll no: {{student.rollno}}</p>
<p>marks(math): {{marks[3]}}</p>
</div>
<script src="http://www.51sjk.com/Upload/Articles/1/0/261/261829_20210702001740953.js"></script>
</body>
</html>
输出
在web浏览器打开textangularjs.html。看到结果如下:

推荐阅读
热点文章
Angular中响应式表单的三种更新值方法详析
7
Angularjs实现下拉框联动的示例代码
7
详解AngularJS跨页面传值(ui-router)
2
详解AngularJS1.x学习directive 中‘& ’‘=’ ‘@’符号的区别使用
3
angular2路由切换改变页面title的示例代码
4
Angular2 组件间通过@Input @Output通讯示例
5
Angularjs中ng-repeat的简单实例
3
AngularJS 中ui-view传参的实例详解
4
浅谈Angular路由守卫
4
详解基于Angular4+ server render(服务端渲染)开发教程
4
