Display the ng-model JSON as the selected option of a dropdown

0

Hello, I'm wondering if this is possible. For example, I have a dropdown with a JSON array of strokes in ng-options :

<select name="curso" ng-options="curso.cursoId.sigla + ' - ' + curso.cursoId.periodo for curso in cursos" class="form-control" ng-model="disciplina.curso" required>
    <option value="">Selecione o curso</option>
</select>

When I edit a course, ng-model disciplina.curso already has a course object, so the idea is that the course that is in that model will already appear selected in the dropdown when I open the form instead of "Select one course. "

Is there any feature of ng-options itself that allows me to do this? Or do I need to create a role or policy? I do not have much experience with AngularJS, so I'm not sure how to proceed.

Thank you in advance!

    
asked by anonymous 29.08.2016 / 01:25

1 answer

0

I was able to do this using track by , that in this case the identifiers of a course are sigla and periodo :

<select name="curso" ng-options="curso.cursoId.sigla + ' - ' + curso.cursoId.periodo for curso in cursos track by curso.cursoId.sigla + curso.cursoId.periodo" class="form-control" ng-model="disciplina.curso" required disabled>
    <option value="">Selecione o curso</option>
</select>

As I read on, using the object id with track by Angular uses this id as the object identifier in the DOM, so it can validate if the object that is in disciplina.curso is equal to any of the objects in the cursos list, and if so, the dropdown will display the ng-model object as the one selected from the list.

    
11.09.2016 / 19:50