我要做的是创建一个从json数据中读取文本的下拉选项。我不知道怎么读不到价值,我不知道为什么。
这是我的代码
我的下拉框:
<label class="item item-input item-select">
<b class="input-label">Claim Type:</b>
<select ng-model="claimType" required>
<option value="Select claim" title="Select Claim" selected disabled>Claim Type</option>
<option ng-repeat="claim in claimType " value="{{claim.value}}"
ng-selected="{{claim.value== claimType}}">
{{claim.text}}
</option>
</select>
</label>
我的Json对象
angular.module('app')
.factory('WebApi', function () {
var claimType = [{
value: "Car",
text: "Car",
}, {
value: "Boat",
text: "Boat",
}, {
value: "Others",
text: "Others"
}]
})
//Display 50 items randomly
var tempData = [];
for (var i = 0; i < 50; i++) {
var selectedClaimType = claimType[Math.floor((Math.random() * claimType.length))];
tempData.push({
claimType: selectedClaimType.text,
})
};
return {
getClaimTypes: function () {
return selectedClaimType.text;
},
}
在我的控制器中,我打电话给
$scope.claimType = WebApi.getClaimTypes();
答案 0 :(得分:0)
您可以使用ng-options
来帮助管理选择列表。
<label class="item item-input item-select">
<b class="input-label">Claim Type:</b>
<select ng-model="claimType" required
ng-options="transport.text for transport in traffic">
<option value="" title="Select Claim" selected disabled>Claim Type</option>
</select>
</label>
这会将选定的transport
绑定到claimType
。如果您希望为claimType
分配值transport.value
,则可以执行以下操作:
<label class="item item-input item-select">
<b class="input-label">Claim Type:</b>
<select ng-model="claimType" required
ng-options="transport.text as transport.value for transport in traffic">
<option value="" title="Select Claim" selected disabled>Claim Type</option>
</select>
</label>