Javascript 使用ng click完全删除或删除元素以及数组中的数据
是否有一种方法可以完全删除或删除元素。我创建的代码只删除元素,而不删除存储元素的数据。基本上,这个概念就像gmail,如果你点击X或者收件人的名字,它会自动从列表中删除Javascript 使用ng click完全删除或删除元素以及数组中的数据,javascript,angularjs,Javascript,Angularjs,是否有一种方法可以完全删除或删除元素。我创建的代码只删除元素,而不删除存储元素的数据。基本上,这个概念就像gmail,如果你点击X或者收件人的名字,它会自动从列表中删除 在Angular中,您通常控制数据本身,而不是显示组件 <span remove-on-click ng-click="removeEmail(e)" class="email-item">{{ e }} <small class="close">X</small></span>
在Angular中,您通常控制数据本身,而不是显示组件
<span remove-on-click ng-click="removeEmail(e)" class="email-item">{{ e }} <small class="close">X</small></span>
scope.removeEmail = function(e) {
var index = scope.$parent.emails.indexOf(e);
scope.$parent.emails.splice(index, 1);
};
对于在其他类型的列表中可重用的指令,您可以考虑将父列表的引用传递给指令,或者将该函数更好地传递给指令。
为什么使用指令来删除电子邮件?var ctrls = angular.module('elstudio.controllers.site');
//Removes Element only
ctrls.directive('removeOnClick', function() {
return {
link: function(scope, elt, attrs) {
scope.removeEmail = function() {
elt.remove();
};
}
}
});
ctrls.controller('ReferralDispatchController', function ($scope, UserService,
ReferralService) {
$scope.emails = [];
$scope.message = '';
$scope.addEmail = function() {
if (!$scope.email) {
$scope.$emit('notify', { message: 'Please provide a valid email address' });
return;
}
// If email already in list, ignore
// FIXME: Provide feedback
if ($scope.emails.indexOf($scope.email) != -1) {
$scope.email = '';
return;
}
$scope.emails.push($scope.email);
$scope.email = '';
};
$scope.sendReferral = function() {
if (!$scope.loginUser) {
$scope.$emit('notify', { message: 'Please sign up or log in to your Electric account.',
duration: 3000 });
angular.element('html, body').animate({ scrollTop: 0 }, 'slow');
angular.element('.login-toggle').click();
return;
}
if ($scope.email != '') {
$scope.emails.push($scope.email);
}
if (!$scope.emails) {
$scope.$emit('notify', { message: 'Please provide at least one email address' });
return;
}
var refer = {
emails: $scope.emails,
message: $scope.message
};
var sendSuccess = function() {
$scope.$emit('notify', { message: 'An invitation has been sent!',
duration: 4000 });
};
var sendFailed = function(error) {
// Retry?
$scope.$emit('notify', { message: "Couldn't send invitation",
duration: 4000 });
};
ReferralService.email(refer).$promise.then(sendSuccess, sendFailed);
};
});
<span remove-on-click ng-click="removeEmail(e)" class="email-item">{{ e }} <small class="close">X</small></span>
scope.removeEmail = function(e) {
var index = scope.$parent.emails.indexOf(e);
scope.$parent.emails.splice(index, 1);
};