简体   繁体   English

Angular ng-select ngModel 更改但 ng-select 未更新

[英]Angular ng-select ngModel changes but ng-select not updating

I have an ng-select looking like this:我有一个 ng-select 看起来像这样:

<ng-select
        class="minimal"
        [items]="productGroupTags"
        [(ngModel)]="selectedProductGroup">
        <ng-template ng-option-tmp let-item="item">
          {{ item.nameI18n | multilanguagePipe }}
        </ng-template>
        <ng-template ng-label-tmp let-item="item">
          {{ item.nameI18n | multilanguagePipe }}
        </ng-template>
      </ng-select>

Where productGroupTags is an array of objects and selectedProductGroup is the selected object. It works, when using the ng-select to chose one of the items within productGroupTags, but doesn't work when setting selectedProductGroup from the outside, eg:其中 productGroupTags 是一个对象数组,selectedProductGroup 是选定的 object。当使用 ng-select 选择 productGroupTags 中的一项时,它有效,但在从外部设置 selectedProductGroup 时不起作用,例如:

this.selectedProductGroup = this.productGroupTags[0];

I'm running angular 11.2.3 and ng-select 6.1我正在运行 angular 11.2.3 和 ng-select 6.1

Any idea why this wouldn't work?知道为什么这行不通吗?

Thank you!谢谢!

Well, turns out I forgot to import the FormsModule.好吧,原来我忘了导入 FormsModule。 Without this, ngModel is not working, but also it does not throw an error.没有这个,ngModel 就无法工作,但它也不会抛出错误。

By default ng-select binds to default label property for display, and keeps whole object as selected value默认情况下,ng-select 绑定到默认的 label 属性进行显示,并保留整个 object 作为选择值

Please check if you are binding an object into the ngModel, and if it is, do check if it of the right type.请检查您是否将 object 绑定到 ngModel 中,如果是,请检查其类型是否正确。

I had the same issue, if you use ng-select inside ngForm you should add the attribute name="yourSelect":我有同样的问题,如果你在 ngForm 中使用 ng-select,你应该添加属性 name="yourSelect":

<ng-select
....
name="name2"
.... >

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM