AngularJS - 在 ng-repeat 中设置选择的默认值 [英] AngularJS - Set default value on select inside a ng-repeat
问题描述
我遇到了 AngularJS 的问题 - 我无法在 中显示选定的值.
这是我的用例:
我有一个视图,它以 ng-repeat 开头来显示组件.每个组件都包含一个选择以选择增值税率.创建新项目时,它看起来不错.但是,当我编辑现有项目时,实际增值税代码未显示在选择中,而是我看到默认选项-- 选择增值税率--"而不是所选的增值税.
我的模型只包含增值税率的 ID.
对于单个组件,我可以使用 $scope 中的变量来设置当前项目值,但在这里我可以有多个组件,每个组件都有自己的增值税率,所以我不知道这里是如何做到的:
这是我的代码
<label>{{i.id}}</label><输入ng-model="i.title"><选择 ng-model="i.vatRateId"><option value="">--- 选择一个选项---</option><option ng-repeat="增值税率值"ng-selected="i.vatRateId == value.id"value="{{value.id}}">{{value.value}}</选项></选择>
和对象:
$scope.vatRates = [{'id':1,'值':'20'},{'id':2,'值':'10'},{'id':3,'值':'7'}];$scope.items = [{ 'id': 1, 'title' : 'Title1', 'vatRateId' : '1' },{ 'id': 2, 'title' : 'Title2', 'vatRateId' : '2' },{'id':3,'title':'Title3','vatRateId':'3'}];
这是一个现场演示来解释我的问题:
我无法为选择正确的值中的每个项目设置.
我不是很清楚你的要求,但如果你想显示一个默认的选择值到 ,你可以使用ng-selected.为了使用它,您需要在您的控制器中将 默认选择值 作为 模型 到您的
并添加 ng-selected到您的
<options ng-selected="{{defaultvalue == value.id}}" ng-repeat="value in vatRates">
为了代码和参考,我只是改变了你的 plunker,
http://embed.plnkr.co/ZXo8n0jE8r3LsgdhR0QP/preview
希望这会有所帮助.
I'm facing to an issue with AngularJS - I'm not able to display the selected value in a <select>
.
Here is my use case:
I have a view which starts by an ng-repeat to display components. Each component contains a select to choose the vat rate. When creating new items, it looks fine. But when I'm editing existing items, the actual vatRate code is not displayed in the select, and instead I see the default option "-- Select VAT Rate --" instead of the selected VAT.
My model only contains the Id of the vat Rate.
With a single component I can use a variable in the $scope to set the current item value, but here I can have multiple components, each of them has its own vat rate, so I am not sure how do that here:
Here is my code
<div ng-repeat="i in items">
<label>{{i.id}}</label>
<input ng-model="i.title">
<select ng-model="i.vatRateId">
<option value="">--- Select an option ---</option>
<option ng-repeat="value in vatRates"
ng-selected="i.vatRateId == value.id"
value="{{value.id}}">{{value.value}}
</option>
</select>
</div>
And the objects:
$scope.vatRates = [
{ 'id': 1, 'value' : '20' },
{ 'id': 2, 'value' : '10' },
{ 'id': 3, 'value' : '7' }
];
$scope.items = [
{ 'id': 1, 'title' : 'Title1', 'vatRateId' : '1' },
{ 'id': 2, 'title' : 'Title2', 'vatRateId' : '2' },
{ 'id': 3, 'title' : 'Title3', 'vatRateId' : '3' }
];
Here is a live demo to explain my issue:
I'm not able to set to each item in the select the right value.
I am not very much clear about your requirement but if you want to display a default selected value to <select>
, you can use ng-selected. In order to use that you need to have default selected value in your controller as a model to your <select>
and add ng-selected to your <options ng-selected="{{defaultvalue == value.id}}" ng-repeat="value in vatRates">
For code and reference i just altered your plunker,
http://embed.plnkr.co/ZXo8n0jE8r3LsgdhR0QP/preview
Hope this helps.
这篇关于AngularJS - 在 ng-repeat 中设置选择的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!