AngularJS - 表示式



表示式用於將應用程式資料繫結到 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 Expressions</title>
   </head>
   
   <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 = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js">
      </script>
      
   </body>
</html>

輸出

在 Web 瀏覽器中開啟 testAngularJS.htm 檔案,檢視結果。

廣告