您现在的位置: 万盛学电脑网 >> 程序编程 >> 脚本专题 >> javascript >> 正文

详解AngularJS中的表达式使用

作者:佚名    责任编辑:admin    更新时间:2022-06-22

   这篇文章主要介绍了详解AngularJS中的表达式使用,包括处理数字和字符串等各种对象的操作,需要的朋友可以参考下

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

  使用数字

  ?

1 <p>Expense on Books : {{cost * quantity}} Rs</p>

  使用字符串

  ?

1 <p>Hello {{student.firstname + " " + student.lastname}}!</p>

  使用对象

  ?

1 <p>Roll No: {{student.rollno}}</p>

  使用数组

  ?

1 <p>Marks(Math): {{marks[3]}}</p>

  例子

  下面的例子将展示上述所有表达式。

  testAngularJS.html 文件代码如下:

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 <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://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script> </body> </html>

  输出

  在Web浏览器打开textAngularJS.html。看到结果如下:

2015616115757530.png (587×339)