我正在尝试从我的简单电话簿应用程序中删除联系人。但是,当我调用 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"
让我的删除过程变得疯狂。如果有人可以建议我如何解决这个问题,我将不胜感激!