Angular 2 not refreshing the view after the value change of a variable?

Tom Šabanov

So, I've been pulling my hair out for the last two hours searching for the answer. I have a simple AppComponent like this:

import {Component, OnInit} from '@angular/core';
import {UserDataService}  from '../services/user-data.service';

@Component({
selector: 'myapp',
template: `
<h1>Angular 2 app inside a desktop app</h1>
<div *ngIf="data">
    {{data}}
</div>
`,
})

export class AppComponent{
     public data : Object;

     constructor(private userDataService : UserDataService) {}

     ngOnInit(){
      this.data=this.userDataService.getUserData(); 
     }

}

What I want is to import the data from a configuration file from UserDataService, which looks like this:

  import {Injectable}    from '@angular/core';
  import {Http,Response} from '@angular/http';
  import {Observable} from 'rxjs/Rx';

  @Injectable()
  export class UserDataService{
   public data : Object;

   constructor(private http : Http){}

   getUserData(){
      this.http.get('./config.json').map((res:Response)=>res.json())
      .subscribe(data => {this.data = data});

   return this.data;
   }

}

I want to check if a certain variable exists in that config file, and if it does I want to show the div, but the problem is the view does not refresh like I thought it would. I clearly am not grasping the situation so can anyone help me?

Günter Zöchbauer

Async processing works a bit different.

export class AppComponent {
     public data : Object;

     constructor(private userDataService : UserDataService) {}

     ngOnInit() {
       // subscribe and assign result when it arrives to `this.data`
       this.userDataService.getUserData().subscribe(data => this.data = data); 
     }

}

Use .map() instead of subscribe() so an Observable is reaturned instead of a Subscription to allow the caller to get notified when the data arrives.

Add an actual return if you want to use the result on call site:

getUserData() {
    return this.http.get('./config.json')
    .map((res:Response)=>res.json())
//    .map(data => {this.data = data});
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Angular 2 View will not update after variable change in subscribe

From Dev

Angular2 View not updating after variable change

From Dev

Angular2 refreshing view on array push

From Dev

View not updating on variable change in Ionic 2 and Angular 2

From Dev

View not getting updated after component variable is changed in Angular2

From Dev

Angular2, view not updating after variable changes in settimeout

From Dev

value not updating in the view after data update , angular2 and typescript

From Dev

Angular 2 refreshing the view without route.navigate

From Dev

Refreshing fullcalendar view on day change

From Dev

View not refreshing after AJAX post

From Dev

View not refreshing after Ajax call

From Dev

Layout not refreshing after orientation change

From Dev

Directive not refreshing after scope change

From Dev

How to update view after change in angular2 after google event listener fired?

From Dev

Angular 2 - Variable in view not updatet

From Dev

$scope is not refreshing after value changed

From Dev

How to watch route change and change value of boolean variable every time route changes in Angular 2?

From Dev

How to watch route change and change value of boolean variable every time route changes in Angular 2?

From Dev

Change variable value and push it after loop

From Dev

Android SQLite Refreshing data on view after modification

From Dev

GridView is not refreshing view after data changed

From Dev

Partial view not refreshing after Jquery delete

From Dev

Angular $parsers is calling after both view change and model change

From Dev

Angular2 component view does not update on value change via method

From Dev

Variable doesn't change value in angular controller

From Dev

Refreshing Fragment with ListView after theme change on container

From Dev

Angular update view without refreshing the page

From Dev

View not refreshing using `this` context in Angular 1.6

From Dev

clear session variable after refreshing PHPpage

Related Related

  1. 1

    Angular 2 View will not update after variable change in subscribe

  2. 2

    Angular2 View not updating after variable change

  3. 3

    Angular2 refreshing view on array push

  4. 4

    View not updating on variable change in Ionic 2 and Angular 2

  5. 5

    View not getting updated after component variable is changed in Angular2

  6. 6

    Angular2, view not updating after variable changes in settimeout

  7. 7

    value not updating in the view after data update , angular2 and typescript

  8. 8

    Angular 2 refreshing the view without route.navigate

  9. 9

    Refreshing fullcalendar view on day change

  10. 10

    View not refreshing after AJAX post

  11. 11

    View not refreshing after Ajax call

  12. 12

    Layout not refreshing after orientation change

  13. 13

    Directive not refreshing after scope change

  14. 14

    How to update view after change in angular2 after google event listener fired?

  15. 15

    Angular 2 - Variable in view not updatet

  16. 16

    $scope is not refreshing after value changed

  17. 17

    How to watch route change and change value of boolean variable every time route changes in Angular 2?

  18. 18

    How to watch route change and change value of boolean variable every time route changes in Angular 2?

  19. 19

    Change variable value and push it after loop

  20. 20

    Android SQLite Refreshing data on view after modification

  21. 21

    GridView is not refreshing view after data changed

  22. 22

    Partial view not refreshing after Jquery delete

  23. 23

    Angular $parsers is calling after both view change and model change

  24. 24

    Angular2 component view does not update on value change via method

  25. 25

    Variable doesn't change value in angular controller

  26. 26

    Refreshing Fragment with ListView after theme change on container

  27. 27

    Angular update view without refreshing the page

  28. 28

    View not refreshing using `this` context in Angular 1.6

  29. 29

    clear session variable after refreshing PHPpage

HotTag

Archive