我正在使用 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>
我的 Controller 代码:
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;
};
}]);
最佳答案
使用 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');
}
};
关于javascript - 获取一次事件后删除 ng-click 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33439606/