【问题标题】:Angular: How to update view when Array changes?Angular:数组更改时如何更新视图?
【发布时间】:2019-05-05 01:19:45
【问题描述】:

我一直在阅读帖子并在谷歌上搜索这个问题的答案,但没有成功

我查看了这两个链接,但什么也没有

Angular doesn't update view when array was changed

Updating HTML view when Angular array changed

我有两个组件(兄弟姐妹) 一个组件添加项目,另一个列出所有添加的项目。这个想法很简单,每当有人添加一个项目时,我都想更新列表以获取该新项目。

export class ListItem implements OnInit {
  list: SomeObject[] = [];

  constructor(private myService: SomeService) {}

  ngOnInit() {
    this.getItems();
  }

  getItems(): void {
    this.myService.getDomesticatedPokemons()
      .subscribe((newItems) => {
        this.list = newItems; // I can see that this line works as expected but the view doesn't get updated though
      });
  }
}

export class AddItem implements OnInit {
  constructor(private myService: SomeService, private list: ListItem) {}

  ngOnInit() {

  }

  saveItem(aForm: NgForm): void {
    // ...
    // ...
    // ...
    // ...

    this.myService.addItem({}).subscribe((result) => {
      if (result.ok === 1) {
        this.list.getItems();
      } else {
        console.error('Error: ...');
      }
    });


  }
}

更新:

这是视图:

<!-- This is the list template -->
<div id="list">
    <div *ngFor="let item of list" class="item">
       ...
       ...
       ...
    </div>
</div>

对于添加组件,有一个表单,saveItem() 在提交时执行

更新 2: 我创建了一个stackblitz 这个例子正在工作,即使我不能(或者至少我不知道如何)重现我的服务从服务器获取数据。我注释掉了主要组件是列表的“更现实”的代码。

【问题讨论】:

  • 不看view/tepmlate/html就无法获得大局
  • @Chellappan 感谢您的链接,我已经尝试过 ChangeDetectorRef.detectChanges() 但没有用。我现在试试另外两个。 benshabatnoam 我按要求添加了 HTML
  • @benshabatnoam:我按要求添加了视图和一些 cmets,:-)
  • 发布一个完整的最小示例作为堆栈闪电战。服务的代码、组件的装饰器是必要的信息。

标签: angular


【解决方案1】:

我已尝试解决您的问题陈述:

要插入列表的组件

查看:

<h3>create list</h3>
Foo:
<input type="text" class="form-field" [(ngModel)]="newItem.foo"><br/>
Bar:
<input type="text" class="form-field" [(ngModel)]="newItem.bar"><br/>
<button class="btn" (click)="addItem()">Add</button>
<button class="btn" (click)="resetForm()">Reset</button>
<hr>

控制器:

export class CreateListComponent  {
  public newItem:Object;

  constructor(private listService: ListService) {
    this.newItem = this.setInital();
  }

  private setInital(){
    return {
      foo: null,
      bar: null
    };
  }
  public addItem(){
    this.listService.addItem(this.newItem);
    this.resetForm();
  }

  public resetForm(){
    this.newItem = this.setInital();
  }
}

显示列表的组件

查看:

<h3>display list</h3>

<div *ngFor="let item of list">
  {{item.foo}} - {{item.bar}}
</div>

控制器:

export class DisplayListComponent implements OnInit, OnDestroy {
  public list = [];
  private subscription: Subscription;

  constructor(private listService: ListService) { }

  ngOnInit() {
    this.getList();
  }

  private getList() {
    this.subscription = this.listService.getFooBarList().subscribe(data => {
        this.list.push(data);
    });
  }

  ngOnDestroy() {
    this.subscription.unsubscribe();
  }
}

由于两个组件处于同一层次结构,我使用 service 和 observable 将数据从创建组件传递到显示组件

服务:

export class ListService {
  private listSubject = new Subject();

  constructor() {}

  public addItem(item: any) {
    this.listSubject.next(item);
  }

  public getFooBarList() {
    return this.listSubject.asObservable();
  }
}

我希望这对你有帮助。 For a working sample please refer this stackblitz.

【讨论】:

  • 感谢@Shrinivas 的回复。一个问题,display-list.component.ts中this.subscription = this.listService.getFooBarList().subscribe(这一行背后的想法或逻辑是什么??
  • 这是关于可观察的。列出流数据(类似于播放某些节目的电视频道)。我正在订阅那个频道。每次流式传输新项目时,我都会收到通知。 this.subscription 是订阅的处理程序。当我不想收到通知时,我将取消订阅该频道。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-22
  • 2019-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多