使用标签选项提取数据

时间:2019-04-29 00:36:55

标签: javascript angularjs

该块显示数据库中的值。选择值后,我要提取ID。

<div class="block">
    <label>Refer</label>
    <select ng-controller="selectRefer">
             <option ng-repeat="ref in refer">{{ref.refer}}</option>
    </select>
    <button ng-click="MyFunction()"> - </button> //тут я бы хотел 
    через ng-click изьять 
    //данные из тега select и передать в 
    //контроллер и 
</div>

控制器

selectInfo.controller('selectRefer', ['$scope', '$http',
  function ( $scope, $http) {
    $http({
      method: 'get',
      url: 'mysite',
    }).then(function successCallback(response) {
      // Store response data
      $scope.refer = response.data;
      $scope.id = response.id
    });
}]);

如何通过单击按钮从选项标签中获取ID-我不明白。

1 个答案:

答案 0 :(得分:1)

您应该使用ng-options而不是ng-repeat

<select ng-controller="selectRefer"
        ng-options="ref.id as ref.name for ref in refer track by ref.id"
        ng-model="selectedRef">
    <option value="">Select</option>
</select>
<button ng-click="MyFunction(selectedRef)"> - </button> 


$scope.MyFunction= function(selectedRef){
    console.log(selectedRef);
}