How to correctly refresh template of component in Angular?

Григорий Грачев

I’m using service with observer subscribing. Here is my InterfaceService:

import { Injectable } from '@angular/core';
import { Subject } from 'rxjs/Subject';

@Injectable()
export class InterfaceService {
  private contentFlag = new Subject();

  public getContent(): any {
    return this.contentFlag;
  }

  public change(): void {    
    this.contentFlag.next(true);  
  }
}

Here is my Component:

import { Component, OnInit } from '@angular/core';
import { InterfaceService } from '../../-services/interface.service';

@Component({
  selector: 'app-main-content',
  template: `<div *ngIf="showContent"> My Content </div>`  // why it is always hidden ?
})

export class MainContentComponent implements OnInit {
  private content$: any;
  public showContent = false;

  constructor(private interfaceService: InterfaceService) {}

  ngOnInit() {
    this.content$ = this.interfaceService.getContent();

    this.content$.subscribe(function(data) {
      this.showContent = data;      
      console.log('Here comes true:', data); // it works - here shows true
    });
  }
}

When other component makes interfaceService.change(), I get ‘true’ in console.

But there is no reaction in Component template. <div *ngIf="showContent"> My Content </div> is always hidden.

What am I doing wrong? Should I refresh template in subscription? How? Or is there a problem with architecture?

AJT82

The problem is that you are losing the scope of this. Use arrow functions to maintain the scope:

this.content$.subscribe((data) => { // here!
  this.showContent = data;      
  console.log('Here comes true:', data); // it works - here shows true
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to refresh template of component by changing variable in other component Angular 13

From

How to Refresh a Component in Angular

From Dev

How to refresh a component when it becomes visible in angular

From Dev

How do I correctly bind a component to refresh when a value is updated?

From Dev

How to refresh the Parent component after a button on child component is pressed? - Angular

From Dev

Angular refresh component

From Dev

Refresh component in angular 2

From Dev

Refresh issue on angular component

From Dev

Angular does not refresh the template

From Dev

How to Mount Correctly an In-DOM Root Component Template

From Dev

How to render a hyperlink in a component template in Angular 6?

From Dev

How to invoke routed component in template in angular

From Dev

How to make a component with a runtime template in Angular?

From Dev

How to add in Angular a template in the Root Component?

From Dev

How to switch component in angular 5 like template?

From Dev

How to get html template of an Angular 2 Component?

From Dev

How to unit test an Angular 1.5 Component template?

From Dev

How to use Spread Operator in angular component template

From Dev

how to reload/refresh dynamically injected component Angular 6

From Dev

How can I 'refresh' a component in Angular without reloading a page?

From Dev

How to reload or refresh only child component in Angular 8

From Dev

Angular select change refresh a component

From Dev

Refresh Content of different Component in Angular

From Dev

Refresh HTML Component Angular 6

From Dev

Angular load component on page refresh

From Dev

How to correctly decide what should be a component in Angular 2?

From Dev

How to correctly pass props to a component with RxJS in Angular 4?

From Dev

Angular 4: How to correctly pass data from component to html

From Dev

Angular 2 Template Component

Related Related

  1. 1

    How to refresh template of component by changing variable in other component Angular 13

  2. 2

    How to Refresh a Component in Angular

  3. 3

    How to refresh a component when it becomes visible in angular

  4. 4

    How do I correctly bind a component to refresh when a value is updated?

  5. 5

    How to refresh the Parent component after a button on child component is pressed? - Angular

  6. 6

    Angular refresh component

  7. 7

    Refresh component in angular 2

  8. 8

    Refresh issue on angular component

  9. 9

    Angular does not refresh the template

  10. 10

    How to Mount Correctly an In-DOM Root Component Template

  11. 11

    How to render a hyperlink in a component template in Angular 6?

  12. 12

    How to invoke routed component in template in angular

  13. 13

    How to make a component with a runtime template in Angular?

  14. 14

    How to add in Angular a template in the Root Component?

  15. 15

    How to switch component in angular 5 like template?

  16. 16

    How to get html template of an Angular 2 Component?

  17. 17

    How to unit test an Angular 1.5 Component template?

  18. 18

    How to use Spread Operator in angular component template

  19. 19

    how to reload/refresh dynamically injected component Angular 6

  20. 20

    How can I 'refresh' a component in Angular without reloading a page?

  21. 21

    How to reload or refresh only child component in Angular 8

  22. 22

    Angular select change refresh a component

  23. 23

    Refresh Content of different Component in Angular

  24. 24

    Refresh HTML Component Angular 6

  25. 25

    Angular load component on page refresh

  26. 26

    How to correctly decide what should be a component in Angular 2?

  27. 27

    How to correctly pass props to a component with RxJS in Angular 4?

  28. 28

    Angular 4: How to correctly pass data from component to html

  29. 29

    Angular 2 Template Component

HotTag

Archive