Ng2-select: Doesn't working when assign data on click event

Created on 27 Jan 2017  路  9Comments  路  Source: valor-software/ng2-select

I want to assign data(colors) on click event but It is doesn't work this moment. so please review my code and let me know. how it's work.

==== TYPESCRIPT CODE ====

public items:Array<any> = [];
addColors(){
     COLORS.forEach((color:{name:string, hex:string}) => {
          debugger
          this.items.push({
              id: color.hex,
              text: `<colorbox style='background-color:${color.hex};'></colorbox>${color.name} (${color.hex})`
          });
          console.log(color.name);
      });

OR

this.items = [
        {'name': 'Blue 1', 'hex': '#AAA'},
        {'name': 'Blue 2', 'hex': '#C0E6FF'},
        {'name': 'Blue 3', 'hex': '#C0E6FF'}
     ];
  }

===== HTML CODE ====

<div style="width: 200px;margin-top: 10px;">
        <ng-select [allowClear]="true"
                   [items]="colors"
                   formControlName="items"
                   (data)="refreshValue($event)"
                   (selected)="selected($event)"
                   (removed)="removed($event)"
                   (typed)="typed($event)"
                   placeholder="No color selected">
        </ng-select>
    </div>
    <div><button type="button" (click)="addColors()">Add Colors</button></div>

@valorkin @Namek @marcalj @NathanWalker @lzoubek @kfbishop

Most helpful comment

I think there are 2 issues in your code.

  1. in your HTML template you should probably have [items]="items"
  2. In order to update ng-select items you cannot just add them into the array that is bound to your template, because this does not trigger a setter on ng-select component. Setter needs to be tiggered, because ng-select internally transforms your items into it's model. see code. So in your (click) code you need to either create new array or you can use EventEmitter together with async pipe. Something like
items$ = new EventEmitter<any[]>();
items = [];
addColors() {   
  // create / update existing array
  items.push({...});
  items$.next(items);
}

[items]="items$ | async"

All 9 comments

@smartHasmukh @valorkin @Namek @NathanWalker @lzoubek

I am facing same issue like this. 18 hours spend in this issue. any solution?

I think there are 2 issues in your code.

  1. in your HTML template you should probably have [items]="items"
  2. In order to update ng-select items you cannot just add them into the array that is bound to your template, because this does not trigger a setter on ng-select component. Setter needs to be tiggered, because ng-select internally transforms your items into it's model. see code. So in your (click) code you need to either create new array or you can use EventEmitter together with async pipe. Something like
items$ = new EventEmitter<any[]>();
items = [];
addColors() {   
  // create / update existing array
  items.push({...});
  items$.next(items);
}

[items]="items$ | async"

I will try it. Thanks

@smartHasmukh do you solve this issues?

Thanks @lzoubek for your answer. Was working on the similar thing and your post helped me to make it work. Greetings to Olomouc from Ostrava!

@wuhemei I will try on monday and will post as well. It will work or not.

for some reason I could not get this to work, it would update the list only once, this answer helped me, and it just works, though it does look more inefficient than having the async pipe. Ah well, Angular noob ;)

I think there are 2 issues in your code.

  1. in your HTML template you should probably have [items]="items"
  2. In order to update ng-select items you cannot just add them into the array that is bound to your template, because this does not trigger a setter on ng-select component. Setter needs to be tiggered, because ng-select internally transforms your items into it's model. see code. So in your (click) code you need to either create new array or you can use EventEmitter together with async pipe. Something like
items$ = new EventEmitter<any[]>();
items = [];
addColors() {   
  // create / update existing array
  items.push({...});
  items$.next(items);
}

[items]="items$ | async"

Yes,its working well,
thanks.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fdu-axiometrics picture fdu-axiometrics  路  5Comments

Cadenei picture Cadenei  路  4Comments

ronzeidman picture ronzeidman  路  3Comments

jay-perera picture jay-perera  路  6Comments

253936563 picture 253936563  路  5Comments