1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

I update a shown value from a subscription, why is my new value not shown?

Discussão em 'Angular' iniciado por Martijn Dirkse, Maio 13, 2025.

  1. I have an Angular component that shows a member variable of its TypeScript class. I subscribe to an rxjs Observable to update the value. I see that my rxjs Subscription is executed, but the new value is not shown. Here is my TypeScript class:

    export class AppComponent implements OnInit, OnDestroy {
    myValue = "Initial";
    title = 'example';
    subject = new Subject<string>();
    subscription: Subscription | undefined;

    ngOnInit(): void {
    const observable = this.subject as Observable<string>;
    this.subscription = observable.subscribe(this.update);
    this.subject.next('New value');
    }

    update(value: string) {
    console.log(`Updating to ${value}`);
    this.myValue = value;
    }

    ngOnDestroy(): void {
    this.subscription?.unsubscribe();
    }
    }


    The shown member is myValue. When I execute this app I see "Initial", not "New value", even though I can see Updating to New value in the console.

    Why don't I see my update?

    Continue reading...

Compartilhe esta Página