Using async pipe with ngFor

TylerDurden

Ultimate goal is to use nested ngFor's created dynamically. I try to create a series of drop-down menus, each depending on the previous one. The exact number of drop-down menus is unknown and created dynamically. Example:

<form [ngFormModel]="dropDownForm" (ngSubmit)="onSubmit()">
    <div *ngFor="#nr of numberOfDropdowns">
      <label>{{nr.name}}</label>
      <select [ngFormControl]="dropDownForm.controls[i]">
          <option  *ngFor="#item of Dropdown[nr.id] | async" value="{{item.value}}">{{item.name}}</option>
      </select>
    </div>
  <button type="submit">Submit</button>
</form>

The whole things fails at Dropdown[nr.id] which does not seem to work with async pipe. I played around a bit:

{{myAsyncObject | async}} //works
{{myAsyncObject['prop1'] | async}} //fails silently
{{myAsyncObject['prop1']['prop2'] | async}} // EXCEPTION: TypeError: Cannot read property 'prop2' of undefined in [null]    

Any ideas on how to get this working?

TylerDurden

OK, managed to solve it myself. Simply create a custom pipe and pass the parameters in. In my case:

import {Pipe, PipeTransform} from 'angular2/core';
@Pipe({
    name: 'customPipe'
})
export class CustomPipe implements PipeTransform {
    transform(obj: any, args: Array<string>) {
        if(obj) {
            return obj[args[0]][args[1]];
        }
    }
}

Then import:

import {CustomPipe} from '../pipes/custompipe'
@Component({
    selector: 'mypage',
    templateUrl: '../templates/mytemplate.html',
    pipes: [CustomPipe],
    directives: [CORE_DIRECTIVES, FORM_DIRECTIVES]
})

and use:

*ngFor="#obj of myAsyncObject | async | customPipe:'prop1':'prop2'"

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Angular 2 using ngFor with the async pipe and index

From Java

Using an array from Observable Object with ngFor and Async Pipe Angular 2

From Dev

Using pipe in *ngFor, the page sometimes updates, sometimes not

From Dev

*ngFor loop with async pipe and hot socket connected observable?

From Dev

Error in Async pipe while using autocomplete

From Dev

Cannot find a differ supporting object ,Async pipe and ngFor problems in angular2

From Dev

How to check for a value in array of strings with ngIf using async pipe?

From Dev

Angular2 Getting very deep nested json value using pipe! *ngFor

From Dev

ngIf container breaks async ngFor

From Dev

"async" pipe not rendering the stream updates

From Dev

Angular async pipe and object property

From Dev

Async Pipe Fails to Render Observable

From Dev

spyOn not working with async pipe in Angular

From Dev

How to consume single Observable property in view from Angular2 component using async pipe?

From Dev

How do I make a Python async named pipe server using curio?

From Dev

Angular : Array.splice in an ngFor with a pipe

From Dev

Using ngFor inside another ngFor?

From Dev

Using async in async function?

From Dev

Using multiple ngFor in tables

From Java

How to handle boolean observables with the async pipe in Angular

From Dev

Async pipe does not fill object data into template

From Dev

Async NamedPipes in case of multiple pipe server instances

From Dev

Error handling with Angular2 async pipe

From Dev

The pipe 'async' could not be found - Angular 11

From Dev

Observable with Async Pipe in template is not working for single value

From Dev

Angular cannot use async pipe with my observables

From Dev

The pipe 'async' could not be found - Angular 11

From Dev

Angular2 async pipe with observable

From Dev

rxjs take operator - to limit results with async pipe

Related Related

  1. 1

    Angular 2 using ngFor with the async pipe and index

  2. 2

    Using an array from Observable Object with ngFor and Async Pipe Angular 2

  3. 3

    Using pipe in *ngFor, the page sometimes updates, sometimes not

  4. 4

    *ngFor loop with async pipe and hot socket connected observable?

  5. 5

    Error in Async pipe while using autocomplete

  6. 6

    Cannot find a differ supporting object ,Async pipe and ngFor problems in angular2

  7. 7

    How to check for a value in array of strings with ngIf using async pipe?

  8. 8

    Angular2 Getting very deep nested json value using pipe! *ngFor

  9. 9

    ngIf container breaks async ngFor

  10. 10

    "async" pipe not rendering the stream updates

  11. 11

    Angular async pipe and object property

  12. 12

    Async Pipe Fails to Render Observable

  13. 13

    spyOn not working with async pipe in Angular

  14. 14

    How to consume single Observable property in view from Angular2 component using async pipe?

  15. 15

    How do I make a Python async named pipe server using curio?

  16. 16

    Angular : Array.splice in an ngFor with a pipe

  17. 17

    Using ngFor inside another ngFor?

  18. 18

    Using async in async function?

  19. 19

    Using multiple ngFor in tables

  20. 20

    How to handle boolean observables with the async pipe in Angular

  21. 21

    Async pipe does not fill object data into template

  22. 22

    Async NamedPipes in case of multiple pipe server instances

  23. 23

    Error handling with Angular2 async pipe

  24. 24

    The pipe 'async' could not be found - Angular 11

  25. 25

    Observable with Async Pipe in template is not working for single value

  26. 26

    Angular cannot use async pipe with my observables

  27. 27

    The pipe 'async' could not be found - Angular 11

  28. 28

    Angular2 async pipe with observable

  29. 29

    rxjs take operator - to limit results with async pipe

HotTag

Archive