1
<div class="text">{{message}}</div>

private message = 'Some message';

ngOnInit() {
    this.setMessageToDisplay();
}   

       setMessageToDisplay() {
          if (this.packageService.isDocPackageEmpty) {
                if(this.router.url === '/documents/todo') {
                    this.message = 'M1';
                }
                else if (this.router.url === '/documents/done') {
                    this.message = 'M2';
                }
                else if (this.imageService.isVisiblePatientDocList()) {
                    let pacientId = this.router.url.split("/")[2];
                    this.message = "M3";
                }
            } else {
                this.message = 'Def Message';
            }
            return this.message;
        }
    }

Using {{message}} displayed intialize value of this variable despite value changed in method this.setMessageToDisplay(). I do something like this in html <div class="text">{{setMessageToDisplay()}}</div> but method is executed few time and I want display only last value of message variable. Is possible set {{message}} in html and refresh view ?

3 Answers 3

2

You can invoke change detection manually with ChangeDetecorRef module :

import {ChangeDetectorRef} from @angular/core

constructor(private ref:ChangeDetectorRef){}

ngOnInit() {
    this.setMessageToDisplay();
    this.ref.detectChanges();
}   
Sign up to request clarification or add additional context in comments.

2 Comments

<div class="text">{{message}}</div> still display initialize value this variable
using console this.ref.detectChanges() is undefined
0

just try this change:

<div class="text">{{message}}</div>

public message = 'Some message';

ngOnInit() {
    this.message = this.setMessageToDisplay();
}   

       setMessageToDisplay() {
          let message: string;
          if (this.packageService.isDocPackageEmpty) {
                if(this.router.url === '/documents/todo') {
                    message = 'M1';
                }
                else if (this.router.url === '/documents/done') {
                    message = 'M2';
                }
                else if (this.imageService.isVisiblePatientDocList()) {
                    let pacientId = this.router.url.split("/")[2];
                    message = "M3";
                }
            } else {
                message = 'Def Message';
            }
            return message;
        }
    }

REMEMBER to set public your message

2 Comments

This is probably good aproach but ngOnInit is executed only one even when i value routerLink changed
use ngAfterViewChecked or better ngAfterContentChecked
0

you can use :

this.ref.detectChanges();

or :

get message() { return this.setMessageToDisplay(); }

Comments

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.