AngularJS - 在选择设置NG重复内部默认值 [英] AngularJS - Set default value on select inside a ng-repeat

查看:148
本文介绍了AngularJS - 在选择设置NG重复内部默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要面临的一个问题,与AngularJS - 我不能够显示在选定值<选择>

I'm facing to an issue with AngularJS - I'm not able to display the selected value in a <select>.

下面是我的使用情况:

我有一个开始由NG重复显示组件的视图。每个组件都包含了一个选择,选择的增值税率。当创建新的项目,它看起来罚款。但是,当我编辑现有项目,实际vatRate code未在选择显示的,而是我看到了默认选项 - 选择增值税税率 - 而不是选定增值税

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.

我的模型只包含增值税税率的ID。

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:

下面是我的code

<div ng-repeat="c in components" ng-form="vatForm">
  <select name="selectedVatRate"
          ng-model="vatRate"
          ng-init="vatRate = v.id"
          ng-change="updateVatRate(c, selectedVatRate)"
          ng-options="v.id for v in vatRates track by v.id">
    <option value="">-- Select VAT Rate --</option>
  </select>
</div>

和对象:

$scope.vatRates = [
    { 'id': 1, 'value' : '20' },
    { 'id': 2, 'value' : '10' },
    { 'id': 3, 'value' : '7' }
];

$scope.components = [
    { 'id': 1, 'title' : 'Title1', 'vatRateId' : '1' },
    { 'id': 2, 'title' : 'Title2', 'vatRateId' : '2' },
    { 'id': 3, 'title' : 'Title3', 'vatRateId' : '3' }
];

演示

Demo

推荐答案

我不是对你的要求很清楚,但如果你想显示默认选定值&LT;选择&GT; ,你可以使用 NG-选择即可。为了使用,您需要有默认选定值在你的控制器作为模式以您的&LT;选择&GT; 并添加NG-选择将&LT;选项NG-选择={{默认值== value.id}}NG重复=值vatRates&GT;

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">

有关code和参考我只是改变了你的plunker,

For code and reference i just altered your plunker,

<一个href=\"http://embed.plnkr.co/ZXo8n0jE8r3LsgdhR0QP/$p$pview\">http://embed.plnkr.co/ZXo8n0jE8r3LsgdhR0QP/$p$pview

希望这有助于。

这篇关于AngularJS - 在选择设置NG重复内部默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆