How to store the variable of an asynchronous callback function

Relisora

I am developing a chrome extension and I am trying to output the selected text on the tab in a textarea inside the plugin.

The function to get the selected text works very well but I can't set the value to the textarea element inside the plugin.

Question: How do I correctly store the value to be able to then pass it to the textarea with data-binding?

HTML:

<div>
    <p>Here will appear the selected text :</p>
    <textarea name="selectedText" id="selectedText" [(ngModel)]="selectedText"></textarea>
    <button (click)="getSelectedText()">Get the selected text</button>
</div>

TS:

export class CaptureComponent {
    selectedText = '';

    getSelectedText() {
        chrome.tabs.executeScript( {
            code: 'window.getSelection().toString();'
        }, function(selection) {
            this.selectedText = selection[0];
        });
    }
}

The selection[0] is working fine so I guess that the way I am trying to store the data is not correct, but I can't seem to find what to change it to.

bugs

In your current approach, this does not refer to your component.

Change your callback to use an arrow function to keep the scope:

getSelectedText() {
   chrome.tabs.executeScript( {
     code: 'window.getSelection().toString();'
     }, (selection) => {
       this.selectedText = selection[0];
    });
}

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 store a value in a variable in a callback function in vuejs

From Java

How to return value from an asynchronous callback function?

From Dev

Meteor: How to catch asynchronous callback function errors

From Dev

How to pass variables to an asynchronous callback function that will remain intact?

From Dev

How to add a callback function to an asynchronous job in powershell and get return data

From Dev

How to store a function in a variable in MATLAB?

From Dev

How To Store Swift function in a variable

From Dev

How To Store Swift function in a variable

From Dev

How to pass variable to a nested callback function

From Dev

How to pass extra variable to callback function in Dart

From Dev

how to access outer variable in Java Callback function

From Dev

How to access a variable in a callback function of PHP flight

From Dev

How to pass variable to callback function in javascript

From Dev

Javascript: How to access local variable in callback function?

From Dev

How to store variable in loss function into instance variable

From Dev

Elegantly pass down variable to asynchronous callback

From Dev

Asynchronous call in for loop cause variable to be wrong in callback

From Dev

how to pass variable from callback function to function call?

From Dev

Add A Callback To A Prebuilt Asynchronous Function Swift iOS

From Dev

How do I use the variable out of its asynchronous function scope

From Dev

How to assign global variable inside of an asynchronous function in Javascript?

From Dev

How to store a function in a variable in Lisp and use it

From Dev

How to store function as class member variable

From Dev

How can I store the result of a function into a variable?

From Dev

how to store function value in variable and display it on textarea

From Dev

How to write a callback function for asynchronous calls from helper method to componentDidMount using React JS

From Dev

Store data from asynchronous call to a variable in AngularJS

From Dev

How can I include a variable inside a callback function?

From Dev

how to read javascript variable from ajax success callback function

Related Related

  1. 1

    How to store a value in a variable in a callback function in vuejs

  2. 2

    How to return value from an asynchronous callback function?

  3. 3

    Meteor: How to catch asynchronous callback function errors

  4. 4

    How to pass variables to an asynchronous callback function that will remain intact?

  5. 5

    How to add a callback function to an asynchronous job in powershell and get return data

  6. 6

    How to store a function in a variable in MATLAB?

  7. 7

    How To Store Swift function in a variable

  8. 8

    How To Store Swift function in a variable

  9. 9

    How to pass variable to a nested callback function

  10. 10

    How to pass extra variable to callback function in Dart

  11. 11

    how to access outer variable in Java Callback function

  12. 12

    How to access a variable in a callback function of PHP flight

  13. 13

    How to pass variable to callback function in javascript

  14. 14

    Javascript: How to access local variable in callback function?

  15. 15

    How to store variable in loss function into instance variable

  16. 16

    Elegantly pass down variable to asynchronous callback

  17. 17

    Asynchronous call in for loop cause variable to be wrong in callback

  18. 18

    how to pass variable from callback function to function call?

  19. 19

    Add A Callback To A Prebuilt Asynchronous Function Swift iOS

  20. 20

    How do I use the variable out of its asynchronous function scope

  21. 21

    How to assign global variable inside of an asynchronous function in Javascript?

  22. 22

    How to store a function in a variable in Lisp and use it

  23. 23

    How to store function as class member variable

  24. 24

    How can I store the result of a function into a variable?

  25. 25

    how to store function value in variable and display it on textarea

  26. 26

    How to write a callback function for asynchronous calls from helper method to componentDidMount using React JS

  27. 27

    Store data from asynchronous call to a variable in AngularJS

  28. 28

    How can I include a variable inside a callback function?

  29. 29

    how to read javascript variable from ajax success callback function

HotTag

Archive