-1

i am working with an angular application in version 10. Below requirement to learn observables with hardcoded json objects with no http calls. For example i have a interface

export interface cities {
cityId: number;
cityName: string;
}

Below is the code where i declare an observable

cities$: Observable<city[]>;

in the Oninit Method i create objects like this

ngOnInit() : void {
 const cityData = [
  {
     cityId: 1,
     cityName: 'Bangalore'
  },
  {
    cityId: 2,
    cityName: 'chennai'
  }
];

// here i want to get the values in the observables. how can i do it
   this.cities$ = ...
}

below is the html code

<ul>
  <li *ngFor='let city of cities$ | async'>
     <span [innerHTML] = 'city.cityName'>
   </li>
</ul>
  • Does this answer your question? [How to create an Observable from static data similar to http one in Angular?](https://stackoverflow.com/questions/35219713/how-to-create-an-observable-from-static-data-similar-to-http-one-in-angular) – jonrsharpe Mar 12 '21 at 10:24

1 Answers1

1

You can use the rxjs of operator like this:

this.cities$ = of(this.cityData);

You will need to import the operator first:

import { of } from 'rxjs';
TotallyNewb
  • 3,884
  • 1
  • 11
  • 16