m_rd_n
m_rd_n

Reputation: 174

Assign array of objects to variable though .subscribe()

I'm following Angular.io guide https://angular.io/guide/router#route-parameters-in-the-activatedroute-service. They used | async which is not clear for me and I decided to make some custimization. I wanted to Assign array of objects to variable though .subscribe() but it doesn't work out((

here is a part of my code

main

ngOnInit() {
this.heroes$ = this.route.paramMap.switchMap((params: ParamMap) => {
  this.selectedId = params.get('id');
  return this.heroService.getHeroes();

});
 this.heroes$.subscribe((heroes: Hero[]) => {
   this.heroes == heroes
   console.log(heroes) // Shows array of Objects
   console.log(this.heroes) // Shows undefined
 })


heroService

getHeroes() { return Observable.of(HEROES); }


HEROES

import { Hero } from './hero';

export const HEROES: Hero[] = [
  { id: 11, name: 'Mr. Nice' },
  { id: 12, name: 'Narco' },
  { id: 13, name: 'Bombasto' },
  { id: 14, name: 'Celeritas' },
  { id: 15, name: 'Magneta' },
  { id: 16, name: 'RubberMan' },
  { id: 17, name: 'Dynama' },
  { id: 18, name: 'Dr IQ' },
  { id: 19, name: 'Magma' },
  { id: 20, name: 'Tornado' }
];

Upvotes: 0

Views: 612

Answers (1)

Andrey
Andrey

Reputation: 559

You made a typo. Your this.heroes == heroes must be this.heroes = heroes instead.

Upvotes: 1

Related Questions