使用AngularJS检测选择列表值的更改

时间:2015-08-05 08:24:14

标签: javascript angularjs

我尝试在ng-change上使用select,但问题是,我需要向方法发送ticket.id,但要ticket.name进行建模。

<select name="ticketAttendeeMapping"
        ng-model="attendee.ticketTypeName"
        class="form-control">
    <option ng-repeat="ticket in inTransitionTickets | filter: greaterThan('countRemaining', 0)"
            ng-value="ticket.name"
            ng-click="onAttendeeTicketmapping(ticket.id, $parent.$index)">
    {{ticket.name}}
    </option>
</select>

我也试过这样做:

<select name="ticketAttendeeMapping"
        ng-model="attendee.ticketTypeName" class="form-control"
        ng-options="ticket.name as ticket.name for ticket in inTransitionTickets | filter: greaterThan('countRemaining', 0)"
        ng-change="onAttendeeTicketmapping(ticket.id, $index)">
    <option value="" disabled>Choose Your Ticket</option>
</select>

3 个答案:

答案 0 :(得分:2)

最简单的方法是只使用模型中的对象:

<select name="ticketAttendeeMapping" ng-model="attendee.ticket" class="form-control" ng-options="ticket.name for ticket in inTransitionTickets" ng-change="change(attendee.ticket.id)">
</select>

然后,您可以使用$scope.attendee.ticket.name访问该名称,并可以使用$scope.attendee.ticket.id

访问该ID

Plunkr

答案 1 :(得分:1)

尝试这样做:

  <select name="ticketAttendeeMapping" ng-model="attendee.ticketTypeName" class="form-control" ng-options="ticket.name as ticket for ticket in inTransitionTickets | filter: greaterThan('countRemaining', 0)" ng-change="onAttendeeTicketmapping(attendee.ticketTypeName, $index)">
        <option value="" disabled>Choose Your Ticket</option>
    </select>

现在您将获得故障单,即ur ng-change方法中的“attendee.ticketTypeName”对象。你可以获得“id”

答案 2 :(得分:0)

我不确定,如果我在这里所做的是OP最初提出的问题。 我们不能在这里使用ng-options !! 因为虽然我们使用“ng-options”,但它将我们对当前所选Json元素的访问限制为ng-options中明确提到的属性,相反ng-repeat使您可以完全访问当前对象。

<select name="ticketAttendeeMapping"
        ng-model="SelectedTicketNameFromngModel"
        class="form-control">
    <option ng-repeat="ticket in ticketJson "
            ng-value="ticket.name"
            ng-click="alertMe(ticket.id)">
    {{ticket.name}}
    </option>
</select>


$scope.SelectedTicketNameFromngModel = '';
    $scope.selectTicketIdFromMethod = '';
    $scope.ticketJson = [{'name':'tick1','id':'t1'},{'name':'tick2','id':'t2'},{'name':'tick3','id':'t3'}];


    $scope.alertMe = function(tId){
               $scope.selectTicketIdFromMethod = tId;
    }  

http://jsfiddle.net/HB7LU/16124/

enter image description here