How to save model manipulation of *ngFor with pipes? - “#item of (result = (items | orderAsc))” doesn't work in A2

a 夏天 提交于 2019-12-05 20:07:27

As far as I know, the old way isn't supported.

I have a workaround for this but I don't know if it's a clean way to do ;-)

You could implement a custom pipe (the dump one in the following sample) you put at the end of your pipe chain to save the filtered value into the component

@Pipe({
  name: 'filter'
})
export class FilterPipe {
  (...)
}

@Pipe({
  name: 'sort'
})
export class FilterPipe {
  (...)
}

@Pipe({
  name: 'dump'
})
export class DumpPipe {
  constructor(@Inject(forwardRef(() => AppComponent)) app:AppComponent) {
    this.app = app;
  }

  transform(array: Array<string>, args: string): Array<string> {
    this.app.save(array);
    return array;
  }
}

@Component({
  selector: 'my-app', 
  template: `
    <div>
      <span *ngFor="#l of (list | filter | sort)">{{l}}</span>
    </div>
  `,
  pipes: [ FilterPipe, SortPipe, DumpFilter ]
})
export class AppComponent {
  constructor() {
    this.list = [ 'n', 'g', 'a', 'u', 'r', 'l' ];
  }

  save(array) {
    console.log('save array = '+array);
  }
}

When the expression is evaluated, the dump pipe calls the save method of the component with the filtered and sorted value.

Here is the corresponding plunkr: https://plnkr.co/edit/xv4BTq7aCyKJSKcA9qB9?p=preview

Hope it helps you, Thierry

According the official documentation it should be:

<tr *ngFor="let item of items | orderAsc as result">{{item.name}} ( {{result.length}} )</tr>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!