-1

here is my angular component -

export class UserListComponent implements OnInit, OnDestroy {
  private _subscriptions: Subscription;
  private _users: User[] = [];
  private _clickableUser: boolean = true;

  constructor(
    private route: ActivatedRoute,
  ) { }

  ngOnInit() {
    const source = interval(1000);
    const users = this.route.snapshot.data['users'] as IUserInterface[];

    this._subscriptions = source.subscribe(() => this._users = users.map(user => new User(user)))

  }

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

i get the data, but the interval does not set it without refreshing.

daniel45
  • 45
  • 4

1 Answers1

0

ngOnInit() is only called once, when a component is first created. This means that your active route is being read only once, which also means that your users array is always the same one.

I'm guessing you are trying to update your component each time the route data gets updated. For that, you need to subscribe to this.route.data (notice I didn't include snapshot). It will look something like this:

this._subscriptions = this.route.data.subscribe(data => this._users = data['users'].map(user => new User(user));
Shy Agam
  • 1,285
  • 1
  • 13
  • 37