KnockoutJS - removeAll() 方法



描述

KnockoutJS Observable removeAll() 方法移除所有專案並將它們作為陣列返回。

語法

arrayName.removeAll()

引數

不接受任何引數。

示例

<!DOCTYPE html>
   <head>
      <title>KnockoutJS ObservableArray removeAll 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 removeAll() method.</p>
      <button data-bind = "click: removeallEmp">Remove All Emps</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.removeallEmp = function() {
               this.empArray.removeAll();
            }
         }

         var em = new EmployeeModel();
         ko.applyBindings(em);
      </script>
      
   </body>
</html>

輸出

讓我們執行以下步驟,瞭解以上程式碼如何工作 −

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

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

  • 單擊刪除所有員工按鈕。

knockoutjs_observables.htm
廣告
© . All rights reserved.