0

I'm 'studying' Angular from a course-book. The example in book is not done by observable, but I have made my example with Json-server and a json file. I would like to update the rating value of the City object within a observable.

I have a city model/object:

export class City {
  constructor(
    public id: number,
    public name: string,
    public province: string,
    public rating: number,
    public highlights?: string[]
  ) { }
}

I have in my controller defined a variable: currentCityJsonServer$: Observable<City>;

When I click on a button the function updateRating is called, which should modify the rating value, with 1 or -1, of the city object:

export class AppComponent implements OnInit {
    
    public currentCityJsonServer$: Observable<City>;
    ...
    updateRating(rating: number): void {
        console.log("This is called from the detail screeen by the function rate via -EventEmitter - met het num:  ", rating);
    
        console.log("before update");
        this.currentCityJsonServer$
        .subscribe(
          data => {
            console.log("..val =", data);
            console.log("..val-name city:", data.name, ' --- rating', data.rating);
          },
          error => console.log("in error", error),
          () => console.log(" observable is now completed.")
        );
        
        console.log("Execute update by adding the rating value");
        this.currentCityJsonServer$.pipe(
          map(currentCityJsonServer => ({...currentCityJsonServer, rating: currentCityJsonServer.rating + rating})),
          tap(currentCityJsonServer => console.log("display the new city rating: ", currentCityJsonServer)) // will display the new city 
        );
        //See if the value has been changed 
        console.log("After update");
        this.currentCityJsonServer$
        .subscribe(
          data => {
            console.log("..val =", data);
            console.log("..specific value val-rating:", data.rating);
          },
          error => console.log("in error", error),
          () => console.log(" observable is now completed.")
        );
    
        console.log("end functie updateRating");
      }
    ...
    }
}

After the update value is still zero.

My question is how can I update/change the value of rating property of the City object with an observable?

3
  • You should use Subject instead of Observable. Commented Jan 23, 2022 at 15:30
  • 1
    Does this answer your question? Typescript Angular - Observable: how to change its value? Commented Jan 23, 2022 at 15:30
  • I've been googling a lot but you mean that update example what I have with map, is not possible? I found this on Stackoverflow and if I look at it, it looks 'logical' to me so I expected that is road to go. I have never used Subject, but I've been googling on it but it is not clear to me. I found also that I could use 'behaviourSubject'. Do you have a simple example? Commented Jan 23, 2022 at 22:01

0

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.