我具有从其他JSON填充的Select选项,并且将Selected值绑定到另一个JSON。使用以下代码,将填充选择,但未选择当前值!。我绝对确定我在选择定义中缺少某些内容。我确实进行了搜索,但是所有这些都与一个对象相关,而不是两个不同的对象。
HTML:
<table class="detailtable"><tbody>
<tr>
<td class="tdItemLabel">Item</td>
<td ><input type=text ng-model="currentItem.ItemName"></td>
<td class="tdItemLabel">Type</td>
<td>
<select ng-model='currentItem.Type' data-ng-options='tType.Type for tType in AllItems'></select>
<td>Qty</td>
<td >{{currentItem.Quantity}}</td>
</tr>
<TR>
<td class="tdItemLabel">Desc</td>
<td colspan="5">
<textarea style='width:100%' rows=6 ng-model='currentItem.ItemDesc'></textarea>
</td>
</tr>
<tr>
<td>Price</td>
<td><input type=text ng-model='currentItem.Cost'></td>
</tr>
</tbody></table>
这是JSON结构
$scope.AllItems = [{Type:"T1",Value:"10"},{Type:"T2",Value:"20"},{Type:"T3",Value:"30"}];
$scope.currentItem = {ItemName:"Picture",Type:"T2",Cost:"200",ItemDesc:"Details of Picture"}
当然,我希望使用currentItem来更新服务器中的记录。
首先,检查选择元素的td
标签,缺少td
标签。
关于这个问题,我认为您需要ng-options
中的语法:
select as label for value in array
根据您的情况:
<select ng-model='currentItem.Type' data-ng-options='tType.Type as tType.Value for tType in AllItems'></select>
尝试将data-ng-options
更改为ng-options
。对于选项,语法为variable.key as variable.value for variable in list
这是我所做的(我发誓,我一个人!)。不知道这是否是Correct方式!
<select ng-model='currentItem.Type' >
<option ng-repeat="tType in AllItems" value='{{tType.Type}}'>{{tType.Type}}</option>
</select>