S
S
seredaes2017-09-20 13:37:21
JavaScript
seredaes, 2017-09-20 13:37:21

How to update template from observable?

Hello.
Help solve the problem.
Here is what I have in the component

export class DashboardComponent implements OnInit {
    username: String = 'hello ';
   
    constructor(private userdata: UserdataService) {  } // здесь у меня сервис, где написано: user = new Subject();
   
 ngOnInit() {
          this.userdata.user.subscribe( (datatest: string) => { 
                   this.username = datatest; 
                   console.log(this.username);  
         });
    }

}

And here's what's in the
{{ username }} template
When I get the data, the correct result is displayed in console.log, but when I make the assignment
this.username = datatest;
the variable does not change its property.
I have a suspicion that this. does not work for a global variable in this case.
Can you tell me how to make the variable change?
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mpnj, 2017-09-20
@mpnj

from the code, it looks like the ngOnInit method only subscribes to the this.userdata.user change.
Those. you are not executing code from subscribe, but simply signing an agreement that you will execute it if the this.userdata.user variable changes.
but the code doesn't show you changing this.userdata.user.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question