如何根据对象属性值选择下拉列表中的值 - 选择和角度

时间:2018-06-12 22:11:33

标签: javascript jquery angular bootstrap-4 jquery-select2

我有一个类别的产品,在编辑产品时,我想在下拉列表中显示当前类别。如果用户想要在下拉列表中更改类别,则可以,但是在加载时我想显示当前产品的选定值。

这是我的HTML

Search.hs:110:31-36: error:
    • Could not deduce (Ord
                          (Vars (DedupingMap (Rep (Index gc)) (IndexedProblem ac))))
        arising from a use of ‘search’
      from the context: (PP gc (IndexedProblem ac),
                         Show (Vars (DedupingMap (Rep (Index gc)) (IndexedProblem ac))),
                         Foldable f, MonadNotify m)
        bound by the type signature for:
                   searchIndexedReplicaProblem :: forall gc ac (f :: * -> *) (m :: *
                                                                                   -> *).
                                                  (PP gc (IndexedProblem ac),
                                                   Show
                                                     (Vars
                                                        (DedupingMap
                                                           (Rep (Index gc)) (IndexedProblem ac))),
                                                   Foldable f, MonadNotify m) =>
                                                  f (Index
                                                       (Clzs
                                                          (PartitionedProblem
                                                             gc (IndexedProblem ac))))
                                                  -> m (Maybe
                                                          (Vars
                                                             (PartitionedProblem
                                                                gc (IndexedProblem ac))))
        at Search.hs:(103,1)-(109,131)
    • In the expression: search
      In an equation for ‘searchIndexedReplicaProblem’:
          searchIndexedReplicaProblem = search
    |
110 | searchIndexedReplicaProblem = search
    |                               ^^^^^^

当我运行我的应用并打开此表单时,您可以看到我将<div class="form-group"> <label class="control-label dash-control-label col-xs-3">Products category:</label> <div class="col-xs-9"> <select id="mainGroupSelectEdit" class="form-control dash-form-control select2" style="width: 100%;" data-minimum-results-for-search="Infinity" name="mainGroupSelectEdit" required (change)="filterSubById(article.groupId)" [(ngModel)]="article.groupId"> <option [value]="helperService.IsItEmptyGuid()" [selected]="isDefaultSelected()">-/-</option> <option [value]="group.id" *ngFor="let group of mainGroups" [selected]="group.id==='a0e25215-a60e-4444-b6ac-4521b7de4b37'">{{group.title}}</option> </select> {{article.mainGroup.id}} </div> </div> &lt; - 它的绑定文章检查是否有类别ID,当我打开表单时它看起来像这样:

enter image description here

如此遗忘{{article.mainGroup.id}}包含一个值,但我无法强制{{article.mainGroup.id}}将该值显示为选中...

任何帮助都会很棒,非常感谢!

编辑: 它什么都不做,只是选择 - / - 如果产品没有组,因为article.mainGroup.id将是空的guid ..

<select>

编辑2:按照Pranay Rana的例子,这就是我所做的:

1。)选择文章 2.)打开模态 3.)模态打开时的图像:

enter image description here

如您所见,值存在,但在模态中未选择任何内容。 这是原始代码,不仅仅是img:

 isDefaultSelected() {
    return this._globalHelperService.isEmptyGuid(this.article.mainGroup.id);
  }

可能select2正在出问题吗?

由于

1 个答案:

答案 0 :(得分:0)

喜欢这个

   <select id="mainGroupSelectEdit" class="form-control dash-form-control select2" style="width: 100%;"
            data-minimum-results-for-search="Infinity" name="mainGroupSelectEdit" required (change)="filterSubById(article.groupId)" [(ngModel)]="article.groupId">
      <option [ngValue]="null" >-/-</option>
      <option [ngValue]="group.id" *ngFor="let group of mainGroups">{{group.title}}</option>
    </select>

在角度[select]中不需要它会根据您传递为[(ngModel)] to select element的ID值选择选项,如果您的值为emptyguid则默认也不需要null选项将被选中