Angular ngModel inside ngFor, with pipe and map, not working

Davide C

I'm having problems in this situation:

@Component({
  selector: 'my-app',
  template: `
    {{items | async| json}}

    <div *ngFor="let item of items | async">
      <input type=checkbox [(ngModel)]="item.b"/>
    </div>
  `
})
export class AppComponent  {
  items = of([{
    name: '1',
  },
  {
    name: '2',
  },
  {
    name: '3',
  }])
  .pipe(map(i=>{
    return i.map(i=>{
      return {
        i: i,
        b: false
      }
    })
  }))
}

Stackblitz app

The problem is that the ngModel is not working and I can't see the b property change. If I remove the map pipe and I put the boolean property in the first array, all is working. Am I missing something? What's the problem?

Thanks

Alexander Staroselsky

You're not doing anything wrong. If you render out {{item.b}} in the ngFor you will see the value is changing between true and false correctly. As mentioned in the other answer this is because of references and change detection. You can also simply save the observable data a property on your class using ngOnInit and subscribe:

import { Component } from "@angular/core";
import { of } from "rxjs";
import { map } from "rxjs/operators";

@Component({
  selector: "my-app",
  template: `
    {{ items | json }}

    <form #myForm="ngForm">
      <div *ngFor="let item of items">
        <input [name]="item.i.name" type="checkbox" [(ngModel)]="item.b" />
      </div>
    </form>
  `
})
export class AppComponent {
  items: any[] = [];

  ngOnInit() {
    this.getData().subscribe(data => (this.items = data));
  }

  private getData() {
    return of([
      {
        name: "1"
      },
      {
        name: "2"
      },
      {
        name: "3"
      }
    ]).pipe(
      map(i => {
        return i.map(i => {
          return {
            i: i,
            b: false
          };
        });
      })
    );
  }
}

Here is an example in action. Don't forget to clean up any observables if needed to avoid memory leaks.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Angular2 ngModel against ngFor variables

Angular 9 ngModel not set radio button value inside ngFor

Angular 2 - 2 Way Binding with NgModel in NgFor

Angular 2 ngModel bind in nested ngFor

Angular 2 - Access/get the input element inside an *ngFor and ngModel

Angular2 bind ngModel from ngFor

*ngFor is not working in angular 2

Set a dynamic property in ngModel inside ngFor, with Angular 2

Angular 2 ngModel inside a function

ngFor with JSON object - Angular 2 ngmodel perspective

angular 4 material show formArrayName with ngFor and ngModel

ngFor not working correctly with ngModel (angular 5.1.2)

Angular ngFor using a Map

Angular 2 NgFor Regex Message Error - [(ngModel)] works but ngModel is undefined

Angular 6 ngModel in ngFor doesn't work

In Angular, using *ngFor inside *ngIf not working

Angular 6 and Angular Material - mat-radio-group [(ngModel)] set dynamic variable inside *ngFor

angular "*ngIf" inside "*ngFor" not working?

Using Pipe on two related variables in Angular ngModel

Angular 2 NgModel+ value inside ngFor doesn't work

ngModel not updating in *ngFor Angular 4

Angular 2 : Search pipe not working when input inside a form tag

Angular 4:ngFor inside html not working

How to communicate between NgModel component and pipe angular

Angular HostBinding in a custom directive not working inside an *ngFor

Object Property named with ? not working inside map in Angular

Countdown timer in Angular pipe not working using pipe and map

Angular - using a date pipe with ngModel

Test a map inside a pipe in Angular (Isolated Test)