我在表中使用ng-repeat来动态生成表行。我需要用"这个"来调用一个函数。用户单击表格中的复选框时的关键字。当我把它作为一个参数传递时,它在js方面未定义。请帮我下面是我的代码
<table class="codelisttab">
<tr><th></th><th>Employee Name</th><th>Employee Id</th></tr>
<tr data-ng-repeat="emp in employees">
<td style="width:20px;"><a data-ng-click="updateEmployeeList(emp.empid, this)" class="ui-multiselect-box commonMultiSelectClass unCatMultiselectColorSelected" id="unCatMulti_109812"></a></td>
<td>{{emp.name}}</td><td>{{emp.empid}}</td>
</tr>
<tr colspan="3"><th style="text-align:center;"><input type="button" data-ng-click="addGroup()" value="Create Group"></th></tr>
My js code:
$scope.updateEmployeeList = function(empid, obj){
var i = $scope.employeeList.indexOf(empid);
if(i <= -1){
$scope.employeeList.push(empid);
obj.style.backgroundColor="#89e3f9";
}
else{
obj.style.backgroundColor="#0e5061";
$scope.employeeList.splice(i,1);
}
console.log($scope.employeeList.toString());
}
答案 0 :(得分:0)
如果您需要函数updateEmployeeList的特殊上下文,您始终可以先绑定特定的上下文,例如:
$scope.updateEmployeeList = $scope.updateEmployeeList.bind(this);
它将在正确的上下文中执行。
但是,这不是很有角度的。
答案 1 :(得分:0)
您对此有何看法?如果你想要元素你需要将$ event传递给ng-click并获取event.currentTarget:get original element from ng-click
<a data-ng-click="updateEmployeeList(emp.empid, $event)" class="ui-multiselect-box commonMultiSelectClass unCatMultiselectColorSelected" id="unCatMulti_109812"></a>
$scope.updateEmployeeList = function(empid, $event){
var i = $scope.employeeList.indexOf(empid),
obj = $event.currentTarget;
if(i <= -1){
$scope.employeeList.push(empid);
obj.style.backgroundColor="#89e3f9";
}
else{
obj.style.backgroundColor="#0e5061";
$scope.employeeList.splice(i,1);
}
console.log($scope.employeeList.toString());
}