表达式用于将应用程序数据绑定到HTML。表达式写在双花括号内,例如{{expression}}中。表达式的行为类似于ngbind指令。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.htm
<html>
<head>
<title>AngularJS表达式</title>
</head>
<body>
<h1>示例应用程序</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 = "https://cdn.staticfile.org/angular.js/1.3.14/angular.min.js">
</script>
</body>
</html>
输出结果
在网络浏览器中打开文件testAngularJS.htm并查看结果。