KnockoutJS - reverse() 方法



描述

KnockoutJS Observable reverse() 方法反轉陣列順序。

語法

arrayName.reverse()

引數

不接受任何引數。

示例

<!DOCTYPE html>
   <head>
      <title>KnockoutJS ObservableArray reverse method</title>
      <script src = "https://ajax.aspnetcdn.com/ajax/knockout/knockout-3.1.0.js"
         type = "text/javascript"></script>
   </head>

   <body>
      <p>Example to demonstrate reverse() method.</p>
      <button data-bind = "click: revEmp">Reverse Array</button>
      <p>Array of employees: <span data-bind = "text: empArray()" ></span></p>

      <script>
         function EmployeeModel() {
            this.empName = ko.observable("");
            this.chosenItem = ko.observableArray("");
            this.empArray = ko.observableArray(['Scott','James','Jordan','Lee',
               'RoseMary','Kathie']);

            this.revEmp = function() {
               this.empArray.reverse();  // reverse order
            }
         }
         
         var em = new EmployeeModel();
         ko.applyBindings(em);
      </script>
      
   </body>
</html>

輸出

讓我們執行以下步驟,來了解上述程式碼的工作原理 −

  • 將以上程式碼儲存在 array-reverse.htm 檔案中。

  • 在瀏覽器中開啟此 HTML 檔案。

  • 單擊反轉陣列按鈕,觀察陣列順序已更改。

knockoutjs_observables.htm
廣告
© . All rights reserved.