阵列更新后,运行方法 [英] Run method after array is updated

查看:113
本文介绍了阵列更新后,运行方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的地方取一些数据形成火力地堡的服务。该服务提供了一个可观察到的,然后我订阅我的组件类。当数据被加载,并将其发送到阵列,用新数据更新DOM,但我需要运行一些javascript和jQuery code再次DOM更新它后,我无法弄清楚如何。

I have a service where I fetch some data form Firebase. That service offers an observable, which I then subscribe to in my component class. When the data is loaded, and sent to the array, the DOM updates with new data, but I need to run some javascript and jQuery code again after the DOM update which I can't figure out how to.

我的服务的方法是这样的:

My service method looks like this:

getSubjects() {
    return new Observable(observer => {
      this.subjectsRef.orderByChild('name').on("value", snapshot => {
        var tempArr = [];
        snapshot.forEach(function(data) {
          var subject = {
            name: data.val().name,
            imgUrl: data.val().imgUrl,
            description: data.val().description,
            teachers: data.val().teachers
          }
          tempArr.push(subject);
        });
        observer.next(tempArr);
      });
    // this.broadcastUpdate();
  });
  }

和我的组件:

export class SubjectsCmp implements AfterViewInit, OnInit, OnChanges {

  constructor(
    private _subjectsService: SubjectsService
  ) {
  }

  ngOnInit() {
    this._subjectsService.getSubjects().subscribe( subjects => this.subjects = subjects);
  }
}

我在的为RxJS文档但无济于事。我想我可以把它改写跟随他们给::例子

I've looked a bit at the documentation for RxJS but to no avail. I thought I could rewrite it to follow the example they give::

var subscription = source.subscribe(
  function (x) {
    console.log('Next: %s', x);
  },
  function (err) {
    console.log('Error: %s', err);
  },
  function () {
    console.log('Completed');
  });

不过,已完成永远不会触发。

But 'Completed' never fires.

2角的onChanges观察员只响应造成的输入事件的变化,这样就不能工作。
基本上我只需要运行的方法填充该数组后,我想我失去了一些东西明显?

Angular 2's onChanges observer only responds to changes caused by input events, so that won't work either. Basically I just need to run a method after the array is populated, I'm thinking I'm missing something obvious?

推荐答案

订阅()方法返回的用户,它继承了添加()方法,你可以使用:

subscribe() method returns Subscriber, which inherits add() method that you can use:

ngOnInit() {
  this._subjectsService.getSubjects()
    .subscribe( subjects => this.subjects = subjects)
    // add new subscription
    .add(() => console.log('Completed', this.subjects));
}

这篇关于阵列更新后,运行方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆