2016-06-28 49 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

相关问题