2013-10-13 51 views
0

我試圖從我的簡單電話簿應用程序中刪除聯繫人。但是,當我調用deletePerson函數時,除了需要的聯繫人之外,我刪除了任何東西。請讓我看看我的錯誤傢伙。角度拼接除了需要的對象從數組中刪除任何東西

我的聯繫人部分:

<ul class="people-list"> 
    <li showhideoptions ng-repeat="person in people | filter: search | 
            orderBy: 'name' | orderBy:'friend':true"> 
     <h4> 
      <span ng-show="person.friend==true" class="icon-star icon-left"></span> 
      <span ng-show="person.friend==false" class="icon-user icon-left"></span> 

     {{person.name}} 

     <span ng-click="deletePerson($index)" 
      class="icon-remove pull-right"></span> 
      </h4> 

    </li> 
</ul> 

我的主控制器廠:

var app = angular.module('contactList'); 

app.factory('simpleFactory', function(){ 
    var people = [ 
      {name: 'Collin', city: 'Omaha', friend: false}, 
      {name: 'Alice', city: 'New York', friend: false}, 
      {name: 'Pasha', city: 'Moscow', friend: true}, 
      {name: 'Denis', city: 'St. Pete', friend: true} 

    ]; 

    var factory = {}; 
    factory.getPeople = function() { 
     return people; 
    }; 

    return factory; 
}); 

app.controller('MainController', function ($scope, simpleFactory) { 

$scope.people = []; 

init(); 

function init() { 
    $scope.people = simpleFactory.getPeople(); 
} 

$scope.addPerson = function() { 
    $scope.people.push(
    { name: $scope.newPerson.name, 
     city: $scope.newPerson.city, 
     friend: false 
    }); 
}; 

$scope.deletePerson = function($index) { 
    $scope.people.splice($index, 1); 
} 
    }); 

UPD

我現在明白了,過濾器

| filter: search | orderBy: 'name' | orderBy:'friend':true" 

使我的刪除過程變得瘋狂。如果有人能夠就如何解決這個問題提供建議,我會很感激!

+0

有什麼不對:http://jsfiddle.net/cherniv/ DARbK /? – Cherniv

+0

@Cherniv礦隨機刪除聯繫人 –

回答

1

您可以刪除使用項目本身,像

HTML

<span ng-click="deletePerson(person)" 
      class="icon-remove pull-right"> 
</span> 

JS

$scope.deletePerson = function(item) { 
    $scope.people.splice($scope.people.indexOf(item), 1); 
} 
+0

謝謝,它的工作。相關問題:http://stackoverflow.com/questions/16266712/remove-item-from-list-after-filtering –

相關問題