我正在使用
例如,如果我单击ID:101,则该函数应仅为此ID调用一次。并且功能将适用于其他ID。换句话说,每个ID只会调用一次该函数。
我的HTML代码:
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;
};
}]);
翻译为:{{链接1:Plunker}}
ng-disabled
禁用,它们会应用disabled
标签,但是点击不会被禁用。如果您希望使用 ng-disabled,则应该使用按钮。如果您实际上没有链接到另一个资源,那么按钮将是最好的选择。虽然有一些 hack 可以使用prevent default
,但是 hack 是不好的做法。 - ste2425