question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Doesn't working when assign data on click event

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 7 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
lzoubekcommented, Jan 27, 2017

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"

0reactions
raghul-selsoftcommented, Jul 3, 2019

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Click Event is not Working When Data loads Dynamic in jquery
i have debug issue find i have used click function on class so when page loads browser assign class to html (click works)....
Read more >
Element: click event - Web APIs | MDN
An element receives a click event when a pointing device button (such as a mouse's primary mouse button) is both pressed and released...
Read more >
Introduction to browser events - The Modern JavaScript Tutorial
An event is a signal that something has happened. All DOM nodes generate such signals (but events are not limited to DOM).
Read more >
HTML Button onclick – JavaScript Click Event Tutorial
The onclick event executes a certain functionality when a button is clicked. This could be when a user submits a form, when you...
Read more >
When a Click is Not Just a Click | CSS-Tricks
The click event is usually tied to a pointer device, typically the mouse, and yet here the Space or Enter key are triggering...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found