Capture radio button of table row using angularjs

时间:2018-01-27 04:58:15

标签: angularjs

I'm new to Angularjs. I'm able to display the data in the front end. But when I try to capture the radio button of each row to delete the record, it gets error.

Here is my html code

<button ng-click="deleteEmployee({{usr.empId}})">Remove Employee</button>
    </div>
    <table border="1">
        <thead>
            <tr>
                <th></th>
                <th>Emp ID</th>
                <th>Name</th>
                <th>Location</th>
            </tr>
        </thead>
        <tbody ng-repeat="usr in users">
            <tr >
                <td><input type="radio" name="empId" value="{{usr.empId}}"></td>
                <td>{{usr.empId}}</td>
                <td>{{usr.name}}</td>
                <td>{{usr.location}}</td>
            </tr>

        </tbody>
    </table>

My angular code is

$scope.deleteEmployee = function (id) {
    // Exception here

    EmployeeService.deleteUser(id)
      .then (function success(response){
          $scope.message = 'User deleted!';
          $scope.user = null;
          $scope.errorMessage='';
      },
      function error(response){
          $scope.errorMessage = 'Error deleting user!';
          $scope.message='';
      })
}

How can I get the id of each row. Any ideas would be very helpful for me.

1 个答案:

答案 0 :(得分:0)

您可以使用ng-model

<button ng-click="deleteEmployee(selectedEmployee)">Remove Employee</button>
    </div>
    <table border="1">
        <thead>
            <tr>
                <th></th>
                <th>Emp ID</th>
                <th>Name</th>
                <th>Location</th>
            </tr>
        </thead>
        <tbody ng-repeat="usr in users">
            <tr >
                <td><input type="radio" ng-model="$parent.selectedEmployee" name="empId" value="{{usr.empId}}"></td>
                <td>{{usr.empId}}</td>
                <td>{{usr.name}}</td>
                <td>{{usr.location}}</td>
            </tr>

        </tbody>
    </table>