Reputation: 2878
i try to contstruct a formulary with angular but displaying data in select element doesn't work ...
formulary.cs.html:
<table style="display: inline-block; float: left; max-width: 510px;">
<tr data-ng-repeat="question in group.questions">
<td>{{question.name}}</td>
<td>
<div ng-switch on="question.control" style="display: inline-block;">
<div ng-switch-when="input">
<input type="text" style="width: {{ question.size }};" data-isnull="{{question.isnull}}" name="{{question.dbcolumn}}" id="{{question.dbcolumn}}" placeholder="{{question.default_value}}" maxlength="{{question.max_length}}" />
</div>
<div ng-switch-when="textarea">
<textarea style="width: {{ question.size }};" data-isnull="{{question.isnull}}" name="{{question.dbcolumn}}" id="{{question.dbcolumn}}" placeholder="{{question.default_value}}"></textarea>
</div>
<div ng-switch-when="checkbox">
<input type="checkbox" data-isnull="{{question.isnull}}" name="{{question.dbcolumn}}" id="{{question.dbcolumn}}" />{{question.default_value}}
</div>
//MY PROBLEM HERE !
<div ng-switch-when="select">
<select style="width: {{ question.size }};" data-isnull="{{question.isnull}}" name="{{question.dbcolumn}}" id="{{question.dbcolumn}}">
<option ng-repeat="choice in question.default_value" value="{{choice.value}}">{{choice.name}}</option>
</select>
<select style="width: {{ question.size }};" data-isnull="{{question.isnull}}" name="{{question.dbcolumn}}" id="{{question.dbcolumn}}" ng-model="question" ng-options="choice.value for choice in question.default_value">
</select>
</div>
<div ng-switch-when="p">
<p style="width: {{ question.size }};" data-isnull="{{question.isnull}}" name="{{question.dbcolumn}}" id="{{question.dbcolumn}}"><b>{{question.default_value}}</b></p>
</div>
<div ng-switch-default>
<!-- default action -->
</div>
</div>
</td>
<td>
<div ng-if="question.unity_variable == true">
<select>
<option ng-repeat="unit in question.unity">{{unit.value}}</option>
</select>
</div>
<div ng-if="!question.unity_variable">
{{question.unity}}
</div>
</td>
</tr>
</table>
I ve looked at Using AngularJS, how do I bind a <select> element to an attribute that updates via a service
in my browser, the label is displayed but not the options in the select.
on the left, method :
<select style="width: {{ question.size }};" data-isnull="{{question.isnull}}" name="{{question.dbcolumn}}" id="{{question.dbcolumn}}" ng-model="question" ng-options="choice.value for choice in question.default_value">
</select>
on the right , method :
<select style="width: {{ question.size }};" data-isnull="{{question.isnull}}" name="{{question.dbcolumn}}" id="{{question.dbcolumn}}">
<option ng-repeat="choice in question.default_value" value="{{choice.value}}">{{choice.name}}</option>
</select>
Can you help me to find my error, please ? THX
Upvotes: 3
Views: 2752
Reputation: 40298
Dont' use ng-repeat
with select/option
. The correct usage, described here is:
<select ng-model="form.question"
ng-options="choice.value as choice.name for choice in question.default_value">
</select>
In order for the selected options to show correctly the choice.value
part must equal the value defined by ng-model
(in the previous example form.question
). I don't know your model, let's consider this:
question.default_value = [
{name: "Alpha", value: "a"},
{name: "Beta", value: "b"}
];
If form.question === "a"
, the 1st option is selected; if form.question === "b"
the 2nd, and so on. You may select the entire object:
<select ng-model="form.question"
ng-options="choice.name for choice in question.default_value">
</select>
In this case initialize the form.question
as:
form.question = question.default_value[0];
// the index, in this example 0, is that of the selected element
In this case the objects must be equal with ===
; it is not enough for their properties to match.
Upvotes: 9