0

I have the following problem. I use angularJS in the chrome extension, in the popup. I would like to get the url of the tab and display it in the popup. For getting the url I use:

var link;

var query = { active: true, currentWindow: true };

function callback(tabs) {
  var currentTab = tabs[0];
  var url = currentTab.url;
  console.log('URL: ' + url);
  link = url
}
chrome.tabs.query(query, callback);
$scope.link = link;
console.log('URL AFTER THE CALL: ' + $scope.link);

This is the code of the MainController that controls popup. I would like to use the url in the popup.html using the $scope.link variable, but I get data only inside of the callback function. The output looks like this:

URL AFTER THE CALL: undefined
URL: http://stackoverflow.com/questions/ask?title=angular%20js%20url%20in%20popup

So it looks like the link assignment and console log 'After call' is executed before the callback function is executed... I do not know how to handle this.

Michael S
  • 92
  • 1
  • 9
  • 1
    you don't need to handle it, all your code depended on link should be in callback function, that is it – saygon91 May 03 '15 at 10:05
  • 2
    possible duplicate of [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](http://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – Xan May 03 '15 at 11:07
  • @saygon91 Have a look. Your input is appreciated by OP. Consider creating an answer yourself. – Yunnosch Jan 22 '21 at 08:55

1 Answers1

2

The comment from saygon91 (credits) I consider a helpful answer.

You don't need to handle it, all your code depended on link should be in callback function. That is it.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
Michael S
  • 92
  • 1
  • 9