2015-05-08 41 views
0

所以我使用destroy()刪除項目從我的$ scope.array單擊。它工作正常,當我打開我的控制檯下面是一個顯示:AngularJS在控制檯上顯示錯誤時,銷燬()調用

$scope.array[index].destroy is not a function 

,所以我刪除此行代碼中,希望它會修復控制檯錯誤,它沒有,但以後我的應用程序不能正常工作。我檢查它是否是別的東西它不是它只是這個,它正在工作,但錯誤仍然顯示。我不知道也許是因爲它是舊版本?

編輯:那個叫上NG-點擊全功能:

$scope.SpliceItems = function (index) 
    { 
      $scope.array.splice(index, 1); 
     } 
    $scope.DestroyItem = function(index) { 

      $scope.array[index].destroy(); 
     } 

    $scope.remove = function(item) 
    { 
    for (var i = 0; i < $scope.array.length; i++) { 
       if ($scope.array[i].Id == item.Id) {  
        $scope.SpliceItems(i); 
        $scope.DestroyItem(i); 

     } 
    } 

EDIT 2

所以,在這裏什麼問題?我需要與此函數動態相乘,添加或從陣列中除去時:

$scope.getTotal= function() { 
     var total = 1; 
     for (var i = 0; i < $scope.array.length; i++) { 
      total *= $scope.array[i]; 
     } 
     if (total == 1) 
      total = 0; 
     return total; 
    } 

當我使用JUST剪接它不會改變總的值,它總是乘法。當我使用splice + destory()它工作的很好,但瀏覽器控制檯給我錯誤,出於某種原因。

+1

你覺得並主張實際的代碼將是有益的? – dfsq

+0

好吧,這是生成控制檯錯誤的行..但如果你堅持 – AlCode

+0

注意:如果你使用ngRepeat的$ index - 這不是一個好主意。這是爲什麼http://codeutopia.net/blog/2014/11/10/angularjs-best-practices-avoid-using-ng-repeats-index/ – tbutcaru

回答

2

使用splice()而不是destroy()例如爲: $scope.array.splice(index, 1);

+0

看看我的編輯 – AlCode

+0

不要使用DestroyItem函數 - 它是不需要。 – Bananan

+0

做拼接從數組中刪除項目? – AlCode

0

如果你想從一個數組中刪除元素試試這個,

$scope.array = ['a', 'b']; 
$scope.array.splice(index,1); 
+0

會嘗試,謝謝 – AlCode

+0

@Orom你不應該對數組使用'delete',永遠不要。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete#Deleting_array_elements。檢查Bananan的答案,這是你應該如何使用拼接。 – dfsq

+0

'delete'操作符刪除對象屬性。技術上數組也是對象,索引是屬性鍵。然而,對於數組來說,更新「長度」也是很重要的,而'delete'不會這樣做。它會導致陣列中的空白。 – dfsq

相關問題