2016-06-28 24 views
0

我正在嘗試做一個數組的更新,我必須首先拼接該元素,然後進行推送。該列表我在一個HTML文件中使用它,我正在使用ng-repeat。爲什麼我的拼接或推動它與AngularJS不工作

vm.editTemplate=function() { 
    var selectedTemplate = localStorage.getItem("selectedTemplate"); 
    localStorage.removeItem("selectedTemplate"); 
    $mdDialog.show({ 
     controller: 'EditTemplateCtrl', 
     controllerAs: 'template', 
     templateUrl: 'views/templatess/addTemplate.html', 
     locals: { 
      template:selectedTemplate 
     } 
    }) 
    .then(function() { }, 
     function(item) { 
      console.log(item); 
      console.log($scope.templatesArray); 
      for (var i = 0; i < $scope.templatesArray.length; i++) { 
      if (item.id == $scope.templatesArray[i].id) { 
       $scope.templatesArray.splice(i,1); 
      } 
     } 
    }); 
} 

在我的HTML文件,我有這個

<div class="hover" 
    ng-repeat="list in templatesArray" 
    ng-click="temp.selectUser(list)" 
    ng-class="{'active': temp.selectedRow.id == list.id}" 
    style=" cursor:pointer;border-bottom:1px solid #fff; margin-bottom:0;" 
    layout-align="space-around center" 
    layout="row"> 
    <span flex="5"></span> 
    <span id="{{list.id}}" flex="90" ng-click="temp.selectTemplate(list)"> 
     {{list.description}} 
    </span> 
    <span flex="5"></span> 
</div> 
+0

是你的兩個'的console.log()'S是叫什麼? – Lex

+0

對不起,我忘了清除它,我只是測試代碼 – Christian

+0

不,這很好 - 我只是想知道它是否被調用。我問,因爲它看起來像你已經把它設置爲錯誤功能。 '.then(function success(){},function failure(){})'是窗體,並且你有一個空函數,後面是帶有代碼的函數。只是想知道爲什麼你必須使用錯誤函數以及它是否實際被調用。 – Lex

回答

0

不要認爲這是很好的做同樣的陣列上的循環內的接頭。此外,如前所述,您應該指定拼接的結果。

所以我寧願做:

1. Find the index, something like following (or with the help of a 
library like underscore.js to avoir to write your own loop) 

var index = -1; 

for (var i = 0; i < $scope.templatesArray.length; i++) { 
    if (item.id==$scope.templatesArray[i].id) { 
     index = i; 
    } 
} 

2. Then splice 

if (index > -1) { 
    $scope.templatesArray = $scope.templatesArray.splice(index,1); 
} 
+0

當我檢查控制檯日誌中的信息,我看到拼接已經donde,但我的html文件列表沒有變化 – Christian

+0

我解決了它,jejeje問題是,我在同一個控制器3倍,所以在某些時候,我的HTML消失的連接。類似的東西。 – Christian

相關問題