When utilizing Web Workers, a new thread is spawned resulting in a separate instance of the Angular app. This means that the code running in the worker is isolated from the main app unless you establish a method of communication such as sessionStorage, localStorage, cookies, or API calls. However, it is crucial to find a way to pass the necessary $scope into the worker.
In general, it is not recommended to have two threads manipulating the same $scope (even if it is achievable). Web Workers are typically employed for intensive tasks independent of the main app, with results passed back to the main app for further utilization (e.g., rendering complex images and returning them for display).
UPDATE
Here is a simplified example of how this can be achieved. The worker script does not use Angular but handles files independently:
Controller
var worker = new Worker('iterator.js');
worker.addEventListener('message', function(e) {
console.log('Worker said: ', e.data);
$scope.final = e.data; // for example
$scope.$apply(); // perform a digest cycle
}, false);
Worker Script
function iterate() {
// Perform iteration and data collection here
// Send the collected data after processing:
self.postMessage(collectedData);
}
You can also send parameters to the worker and call the iterate function with arguments.
The examples provided were derived from http://www.html5rocks.com/en/tutorials/workers/basics/ to serve as a guide on the implementation. Utilize workers for resource-intensive tasks and only transmit necessary data back to the main app.