表达式作为角点击方法内的参数

时间:2015-11-08 12:01:05

标签: javascript html angularjs java-ee

在下面的代码中,我需要将票证ID作为方法参数。 ng-click =" rejectTicket({{ticket.id}})" (当然此代码不起作用)

我想将生成的表行中特定票证的id发送到我的后端java控制器并调用reject()方法。任何想法如何实现此功能" clean "方式是什么?

<tr ng-repeat="ticket in tickets" ng-show="ticket.state === 'NEW' || ticket.state === 'PROCESSING'">
    <td>{{ticket.created}}</td>
    <td style="text-align: center;">
        <a href="#" ng-click="rejectTicket({{ticket.id}})">
            <img class="ico" src="../lib/images/reject.png" title="Reject ticket"> &nbsp;
        </a>
        <a href="#" ng-hide="ticket.state === 'PROCESSING'">
            <img class="ico" src="../lib/images/incrase.png" title="Change to processing"> &nbsp;
        </a>
        <span ng-show="ticket.state === 'PROCESSING'">
            <img class="ico" src="../lib/images/incraseDisabled.png" title="Already processing"> &nbsp;
        </span>
        <a href="#">
            <img class="ico" src="../lib/images/approve.png" title="Satisfy ticket">
        </a>
    </td>
</tr>

编辑1 控制器代码如下所示。

angular.module('app').controller('TicketController', function ($scope, $http, $location) {
    $scope.tickets = [];

    /**
     * Get tickets.
     */
    $http.get('rest/tickets').then(function (response) {
        $scope.tickets = response.data;
    });

    /**
     * Create a ticket.
     */
    $scope.createTicket = function () {
        console.log('submit');
        $http({
            url: 'rest/tickets',
            method: "POST",
            data: $scope.ticket
        }).then(function (response) {
            $scope.tickets.push(response.data);
        }, function () {
            alert('Error: New ticket was not created.');
        }).finally(function () {
            $scope.ticket = {
                state: 'NEW'
            };
        })
    };

    /**
     * Reject a ticket.
     */
    $scope.rejectTicket = function (ticketId) {
        console.log('submit');
        $http({
            url: 'rest/tickets/reject',
            method: "PUT",
            data: ticketId
        }).then(function (response) {
            $scope.tickets.push(response.data);
        }, function () {
            alert('Error: Can not reject this ticket.');
        }).finally(function () {
            $scope.ticket = {};
        })
    };
});

1 个答案:

答案 0 :(得分:0)

您在ngClick中不需要{{}}表示法,所以这一行:      <a href="#" ng-click="rejectTicket({{ticket.id}})"> 应改为:      <a href="#" ng-click="rejectTicket(ticket.id)">