I am trying to emit a subject while fetching data from another service ,but the subject is not passing the value from setRecipe() to getRecipe() and in ngOninit of the component there is not value getting from the service.
@Injectable()
export class RecipeService{
recipesChanged = new Subject<Recipe[]>();
private recipes:Recipe[]=[];
constructor(private slService:ShoppingListService){}
setRecipes(recipe: Recipe[]){
this.recipesChanged.next(this.recipes.slice());
console.log("value from setRecipe()", this.recipes);
}
getRecipes(){
console.log("value from getRecipes()", this.recipes.slice());
return this.recipes.slice();
}
in the RecipeListComponent
export class RecipeListComponent implements OnInit, OnDestroy {
recipes:Recipe[];
subscription: Subscription;
constructor(private recipeService : RecipeService ,
private router : Router ,private route : ActivatedRoute
) {}
ngOnInit() {
this.recipeService.recipesChanged
.subscribe(
(recipes: Recipe[]) => {
console.log("recipes from recipe list comp",recipes);
this.recipes = recipes;
}
);
console.log("my recipes",this.recipes);
this.recipes= this.recipeService.getRecipes();
}
and the data is fetched from firebase in datastorage service and is setting to the setRecipe() of recipe service. I am getting value of recipes from Firebase to datastorage service and in the setRecipe()log I am getting value but its not setting the value of my recipes[] and so it is not passing value to the component.