0

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' }
];

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.