Select option to select otherwise with AngularJS

1

I recently found this example in JSFiddle to select given option in a select element.

I noticed that it is selected as follows:

HTML :

<div class="listitem" ng-repeat="Choice in Person.Choices">
     {{Choice.Name}}: 
     <select 
        ng-model="Choice.SelectedOption"                 
        ng-options="choice.Name for choice in Choice.Options track by choice.ID"></select>
    {{Choice.SelectedOption.ID}}
</div>

JSON :

{
 "Name":"Dinner",
 "Options":[{Name:"Fish",ID:1}, {Name:"Chicken",ID:2}, {Name:"Beef",ID:3}],
 "SelectedOption":{Name:"Chicken",ID:2} // Neste trecho
}

Doubt :

I can not create a global variable inside my controller like this:

$scope.selected;

And put the value of ng-model of element select with selected and pass the value of ID that I want simply without doing the way you are doing? That is, passing the value of option I want instead of a JSON object.

Are there other ways?

    
asked by anonymous 10.02.2016 / 19:31

1 answer

1

If I understood correctly, yes - just change

ng-model="Choice.SelectedOption"   

for

ng-model="selected"

and the directive will use $ scope.selected as the template aspect to undergo binding .

    
10.02.2016 / 19:43