我尝试用角度来构建一个处方集但是在select元素中显示数据并不起作用......
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>
我看了Using AngularJS, how do I bind a <select> element to an attribute that updates via a service
在我的浏览器中,显示标签,但不显示选择中的选项。
左边的方法:
<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>
在右边,方法:
<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>
你能帮我找一下我的错误吗? THX
答案 0 :(得分:9)
请勿ng-repeat
使用select/option
。描述here的正确用法是:
<select ng-model="form.question"
ng-options="choice.value as choice.name for choice in question.default_value">
</select>
为了使所选选项正确显示,choice.value
部分必须等于ng-model
定义的值(在前面的示例form.question
中)。我不知道你的模型,让我们考虑一下:
question.default_value = [
{name: "Alpha", value: "a"},
{name: "Beta", value: "b"}
];
如果form.question === "a"
,则选择第一个选项;如果是form.question === "b"
第二个,依此类推。您可以选择整个对象:
<select ng-model="form.question"
ng-options="choice.name for choice in question.default_value">
</select>
在这种情况下,将form.question
初始化为:
form.question = question.default_value[0];
// the index, in this example 0, is that of the selected element
在这种情况下,对象必须与===
相等;它们的属性不足以匹配。