How to get the selected option in angular <select>

Trying to figure out how to get the id and name of the selected element in angular. Mostly new to js and angular, so not quite sure what I'm doing wrong. I'm sure something simple is missing me.

I have an html element that looks like this:

 <md-input-container>
     <label>Difficulty</label>
     <md-select ng-model="difficulty" ng-change="update()">
         <md-option ng-repeat="difficulty in difficulties">
             {{difficulty.Name}}
         </md-option>
    </md-select>
</md-input-container>

Calling api to get list of difficulties returns next json

[{DifficultyID: 1, Name: "Easy"}, {DifficultyID: 2, Name: "Medium"}, {DifficultyID: 3, Name: "Hard"}]

In my controller, I defined a method

$scope.update = function () {
            console.log($scope.item.Name);
        }

Always get undefined for a name.

+4
source share
2 answers

ng-valuemay be useful in this case. See the Plunker Section .

<md-select ng-model="difficulty">
     <md-option ng-value="opt" ng-repeat="opt in difficulties" ng-click="update(opt)">
         {{opt.Name}}
     </md-option>
</md-select>

opt, difficulty. ng-click ng-change md-select

+3

, ng-.

,

$scope.difficulty 

.

, ?

$scope.$watch('dificulty',function(newVal,oldVal){
  console.log($scope.dificulty);
});

. , . , ng-change .

,

+2

Source: https://habr.com/ru/post/1629930/


All Articles