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.

How to set bindLabel when using pipe?

See original GitHub issue

I have this in my html, needed to use ng-template because of pipe.

    <ng-select [items]="codeList" [virtualScroll]="true"
                      [(ngModel)]="selectedCode" 
                      bindValue="code" name="name-code" required>
            <ng-template ng-option-tmp let-item="item">
                <div title="item">{{item | translateCode: (infoService.getCurrentLanguage())}}</div>
            </ng-template>
    </ng-select>

List of options in dropdown is showing ok, but when I select some option it doesn’t shows. Of course, search is not working either.

How is possible to call function for translation of every option/item so that ng-select works okay? One way would be function in bindLabel, but for now I can see it only accepts string.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
majadinjarcommented, Apr 21, 2021

I found this solution:

<ng-select [items]="codeList" [virtualScroll]="true"
                      [(ngModel)]="selectedCode" 
                      [searchFn]="searchCode" [compareWith]="byCode"
                     name="name-code" required>
            <ng-template ng-option-tmp let-item="item">
                <div title="item">{{item | translateCode: (infoService.getCurrentLanguage())}}</div>
            </ng-template>
</ng-select>

Add two functions - searchFn and compareWith (see ng-select docs for explanation).

0reactions
keivansahebdelfardcommented, Mar 3, 2021

I’m running into this same issue now. I’m assuming there’s no way to use a pipe on the bindLabel?

In this instance I’m passing an array of enum values to items. I have a pipe that translates the enum to something more presentable to the user, so really in this case specifically I actually don’t need the bind label but I do need to transform the displayed values.

I know I could use a vanilla instead and achieve the results I want but I’m trying to implement this in a reusable pattern with other arrays of various types. This might be a little too ambitious though.

hope this work

export enum Smpl{
  Name1 = 1,
  Name2 = 2,
  Name3 = 3,
}

export const SmplLbl = new Map<number, string>([
  [Smpl.Name1, "otherName1"],
  [Smpl.Name2, "otherName2"],
  [Smpl.Name3, "otherName3"],
]);

export interface NgCombo {
  value: number;
  label: string;
}

 enumDS(T1, T2) {
    var _res: NgCombo[] = [];

    for (let key in T1) {
      if (!isNaN(Number(key))) {
        _res.push({ label: T2.get(parseInt(key)), value: parseInt(key) });
      }
    }

    return _res;
  }

using :

 this.ngSelectDataSource = this.sharedSv.enumDS(Smpl, SmplLbl);
Read more comments on GitHub >

github_iconTop Results From Across the Web

Use pipe on bindLabel at <ng-select> - Stack Overflow
It seems like you have to use a template, try: <ng-select [items]="prozessItem$ | async" [multiple]="true" bindValue="nrItems" > ...
Read more >
How to set bindLabel when using pipe? - Bountysource
How to set bindLabel when using pipe ?
Read more >
Ng Select - StackBlitz
bindLabel ="name". placeholder="Select city". [(ngModel)]="selectedCity"> ... <label>Multiselect with custom. bindings</label>. <ng-select [items]="cities2".
Read more >
bind label in a grid view - MSDN - Microsoft
Object reference not set to an instance of an object. can you please help me with this? Sunday, July 13, 2008 3:30 AM ......
Read more >
Data sources
Backend data with async pipe ... You can also set array of objects as items input ... you may want to set simple...
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