在获得一次事件后删除ng单击事件
Remove ng-click event after getting one time event
我将ng-click
函数与ng-repeat
一起使用。但我想为特定的id获得一次事件。之后,如果我们点击该id,则不应该为该id调用函数。
例如,如果我点击id:101,那么应该只为这个id调用一次函数。而函数将适用于其他id。换句话说,函数将为每个id调用一次。
我的HTML代码:
<body ng-controller="AppController">
<table>
<tr ng-repeat="user in users" class="table table-striped table-bordered">
<td>{{user.firstname}}</td>
<td>{{user.lastname}}</td>
<td><a href="javascript:void();" ng-class="{true:'shortlisted',false:'shortlist'}[shortlistStatus == {{user.id}}]" ng-disabled="disable_{{user.id}}" ng-click="shortlist(user.id);" >Shortlist</a></td>
</tr>
</table>
</body>
我的控制器代码:
var app = angular.module('plunker', []);
app.controller('AppController', ['$scope', function($scope) {
$scope.users = [{
firstname: "John",
lastname: 'Doe',
id: "101"
}, {
firstname: "John",
lastname: 'Doe2',
id: "102"
}, {
firstname: "John",
lastname: 'Doe3',
id: "103"
}];
$scope.shortlist = function(val) {
alert(val);
$scope.shortlistStatus = val;
var test = 'disable_' + val;
$scope.test = true;
};
}]);
Plunker
使用id数组并检查:
$scope.clickedId = [];
$scope.shortlist = function(val) {
if($scope.clickedId.indexOf(val) < 0) {
alert(val);
$scope.clickedId.push(val);
$scope.shortlistStatus = val;
var test = 'disable_' + val;
$scope.test = true;
} else {
alert('just clicked');
}
};
您可以稍微更改您的结构,因为您有几个问题。
不能禁用第一个锚点。您可以应用禁用的标记,并将锚点设置为禁用的样式,但单击事件仍将触发。
如果你正在执行静态操作,比如不链接到另一个资源,比如一个单独的页面,你真的应该使用一个按钮,然后可以设置样式并禁用它。
第二个问题是ng-disabled
表达式的计算不正确。该指令将其表示为truthy(禁用)或falsy(启用)值。
因此,考虑到这一点,我建议:
将HTML更改为使用按钮,然后将用户模型扩展为具有禁用标志。还要将整个用户对象传递到单击中。
<button ng-class="{true:'shortlisted',false:'shortlist'}[shortlistStatus == {{user.id}}]" ng-disabled="user.disable" ng-click="shortlist(user);" >Shortlist</button>
在单击中,根据需要使用用户对象,并将禁用标志设置为true。
$scope.shortlist = function(user){
alert(user.id);
$scope.shortlistStatus = user.id;
user.disable = true;
};
Full plunker:http://plnkr.co/edit/R2Ip8rLkDislYx4Z6nXR?p=preview
希望对有所帮助
这里有一个更简单的解决方案:
app.controller('AppController', ['$scope', function ($scope) { $scope.users = [ {firstname: "John", lastname: 'Doe', id: "101", shortlisted: false}, {firstname: "John", lastname: 'Doe2', id: "102", shortlisted: false}, {firstname: "John", lastname: 'Doe3', id: "103", shortlisted: false} ]; $scope.shortlist = function (user) { user.shortlisted = true; }; }]);
<body ng-controller="AppController">
<table>
<tr ng-repeat="user in users" class="table table-striped table-bordered">
<td>{{user.firstname}}</td>
<td>{{user.lastname}}</td>
<td><a href="#" class="shortlist" ng-hide="user.shortlisted" ng-click="shortlist(user);">Shortlist</a>
<span ng-show="user.shortlisted" class="shortlisted">ShortListed</span></td>
</tr>
</table>
</body>
除了简化代码外,链接被span取代,因此它不再可点击。我相信这将有更好的用户体验。这是plunker
相关文章:
- 兰吉 |如何在 span 标签上使用单击事件删除突出显示的文本
- javascript删除事件侦听器
- 从dom中删除任何元素后,Touchmove事件停止触发
- fullcalendar在删除导致浏览器挂起的多个事件时速度较慢
- 使用jQuery删除动态创建的元素上的mouseenter事件
- IE9-添加和删除DOM元素会破坏父keydown事件
- 如何在不删除类似侦听器的情况下从父对象中删除jQuery事件侦听器
- 选择选项是添加或删除事件
- 删除在特定事件上设置的样式
- jQuery使用.of()删除事件处理程序时出现的问题
- 可以clearTimeout删除JavaScript中已触发超时事件的未处理回调
- 如何删除可拖动组中某个元素上的拖动事件
- 删除Google Calander API中的事件
- fullcalendar后台事件:允许删除
- 删除所有按下的按键事件
- 分配和删除单击事件
- jquery on事件删除除span之外的所有标记
- 根据事件有条件地阻止事件删除
- HTML - 粘贴事件删除多余的空格
- 事件删除搜索下拉列表