0

In the [data] attribute in the object tag, a method is called that returns a link to a PDF, which then the browsers default PODF viewer. The problem that I am encountering is that the PDF viewer keeps flashing because the method keeps getting called and returning the URL. Is there a way I can only fire this method once?

 <div *ngSwitchCase="'pdf'"
       id="pdf"
       class="pdf">

    <object style="width: 100%; height: 100%;"
            [data]="returnEncodedLink()"> </object>

  </div>
1
  • 1
    call this method in your .ts and store response in a variable and pass that variable in this [data] attribute. Commented Jun 16, 2022 at 9:33

2 Answers 2

1

Every time change detection fires it will check all variables in your template to see if they have changed.

If you provide a function, change detection will need to call the function in order to see if it returns a different value.

If you only want your function to be called once, then just call it in the ngOnInit lifecycle hook, and assign the result to a variable. Then change detection can simply check the identity of this variable rather than calling your function.

In component ts file

encodedLink = '';

ngOnInit(){
   this.encodedLink = this.returnEncodedLink();
}

html

<div *ngSwitchCase="'pdf'" id="pdf" class="pdf">
  <object
    style="width: 100%; height: 100%"
    [data]="encodedLink"
  ></object>
</div>
Sign up to request clarification or add additional context in comments.

Comments

1

Use getter method and it will be only trigger if its return value changed. Ts file:

encodedLinkData = ''
...
get returnEncodedLink(){
return encodedLinkData
}

Html file:

 <div *ngSwitchCase="'pdf'"
       id="pdf"
       class="pdf">

    <object style="width: 100%; height: 100%;"
            [data]="returnEncodedLink"> </object>

  </div>

2 Comments

sorry for mistake ..I have edited it to [data]="returnEncodedLink"
good approach to use getter function, that's a good way to Intercept every change automatically.

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.