【问题标题】:How to insert values in Observable type of array in ng-select如何在 ng-select 中的 Observable 类型的数组中插入值
【发布时间】:2019-03-03 15:36:51
【问题描述】:

我正在使用ng-select,特别是使用 typeahead 从服务器获取列表作为用户类型。以下示例如typeahead example : people3$ 中所述。我的要求是在用户单击按钮时增加价值(我有 idlabel 和相应的对象)但我无法理解:

  1. 如何将 Item 推送到 item : Observable<any[]> 我尝试执行 this.item = concat(of([{label:this.field.display,id:value}])) 它会推送值,但它不会反映 ng-select 中的值,直到我单击元素外的其他位置。
  2. 我试过this solution by @ViewChildthis.ngSelect.items = [{label:this.field.display,id:value}] 仍然没有在ng-select 中显示价值。

在组中添加值后,我添加了对象但它仍然没有显示,如果我点击任何地方很快就会显示值。

我想知道

  1. 这种行为可能是什么原因。
  2. Observable<any[]>中推送项目的首选方式

这是我的 ng-select 配置:

<ng-select class="form-control ng-select-padding"
                 [items]="referenceItems | async"
                 [hideSelected]="true"
                 [loading]="searchFailed"
                 [typeahead]="searchReferenceItems"
                 bindLabel="label"
                 bindValue="id"
                 (change)="setField($event)"
                 (blur)="onUpdated()"
                 [formControlName]="field.name" ngDefaultControl>
      </ng-select>

//@ TS file

searchReferenceItems =  new Subject<object>();
referenceItems: Observable<any[]> ;

感谢您的帮助

【问题讨论】:

  • 你的控制台有错误吗?
  • 不,没有这样的错误
  • “它会推送值,但它不会反映 ng-select 中的值,直到我单击元素外的其他位置。”可观察到的 ISN'T 值。将其视为“占位符”,让您知道“发生某事时”。 “什么都没有改变”,直到“有事发生”!问:有意义吗?

标签: angular rxjs angular6 rxjs6 angular-ngselect


【解决方案1】:

下一个函数必须通知可观察对象,您可以使用 BehaviorSubject 和函数 asObservable() 来显示元素。

   subject = new BehaviourSubject<any>([]) 
   referenceItems = this.subject.asObservable(); 

然后,当您想要更新该值时,您可以对主题使用“下一个”功能。

   this.subject.next(this.subject.value.concat({id:..., label:...}));

【讨论】:

  • 收到了吗?
  • 嗨,是的,我试过了,但没有运气会尝试分享相同的 stackblitz 示例
  • 好的,分享一下stackblitz
  • @AbelValdez 出现拼写错误:“BehaviorSubject”不是“BehaviourSubject”
猜你喜欢
  • 2021-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-08
  • 2019-06-21
  • 2019-10-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多