如何从dropdown ng-change事件中传递值

时间:2016-12-23 22:47:54

标签: angularjs angularjs-directive angularjs-scope

我想要car_id如何从下拉列表中获取car_id并传递给ng-change事件请帮帮我

 <select   ng-change="carsCtrl.ok(car_id)">
     <option ng-repeat="item in carsCtrl.current_job.cards">
  {{item.brand}}   {{item.last_digits}}
     </option>
 </select>

1 个答案:

答案 0 :(得分:2)

您需要的第一件事是为ng-change添加ng-model指令。之后,您可以在ng-model中添加要在ng-change指令中调用的函数中的变量,例如:

HTML:

<select ng-change="check()" ng-model="cardSelected"><option ng-value="item.id" ng-repeat="item in cards">{{item.brand}}</option></select>

控制器:

$scope.cards = [
    {id: 1, brand: 'Brand1'},
    {id: 2, brand: 'Brand2'},
    {id: 3, brand: 'Brand3'}        
];

$scope.check = function () {
  console.log('cardSelected', $scope.cardSelected);
};