0

the block displays the value from the database. After selecting the value I want to pull out the 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>

Controller

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
    });
}]);

How to get the id out of the options tag by clicking on the button - I don’t understand something.

georgeawg
  • 48,608
  • 13
  • 72
  • 95
  • Use the [ng-model directive](https://docs.angularjs.org/api/ng/directive/ngModel) to bind selection to controller. For more information, see [AngularJS ` – georgeawg Apr 29 '19 at 03:29

1 Answers1

1

You should use ng-options instead of 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);
}
georgeawg
  • 48,608
  • 13
  • 72
  • 95
Hien Nguyen
  • 24,551
  • 7
  • 52
  • 62