Select option in select otherwise with Angularjs

Asked

Viewed 748 times

1

I recently found that example in Jsfiddle to select particular option in one element select.

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’t create a global variable within my controller that way:

$scope.selected;

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

There are other ways?

1 answer

1

If I understand correctly, yes - just change

ng-model="Choice.SelectedOption"   

for

ng-model="selected"

and the Directive will use $Scope. as the aspect of the model to suffer Binding.

  • I was doing exactly as you said @Ibotinelly, I’m using Angularjs 1.5, but when passing only the oid(identifier) for the aspecter of the model select did not change. But when I passed the whole object, with the oid and the descricao contained in the ng-options, worked properly. $scope.objeto.oid = 1 and $scope.objeto.descricao = "valor".

Browser other questions tagged

You are not signed in. Login or sign up in order to post.