如何在 AngularJS 中使用 Web Worker? [英] How to use a Web Worker in AngularJS?

查看:52
本文介绍了如何在 AngularJS 中使用 Web Worker?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 AngularJS Seed,我想看看 Web Worker 的工作实现.

I'm using AngularJS Seed and I want to see a working implementation of a Web Worker.

我想让一个简单的 Web Worker 工作以便理解它,但我在功能上遇到了问题.

I want to make a simple Web Worker work in order to understand it, but I'm running into an issue with the functionality.

我在 services.js 中有 Web Worker 代码,如下所示:

I have the Web Worker code in the services.js like so:

'use strict';

/* Services */
var app = angular.module('myApp.services', []).

app.factory("HelloWorldService",['$q',function($q){

    var worker = new Worker('js/doWork.js');
    var defer;
    worker.addEventListener('message', function(e) {
      console.log('Worker said: ', e.data);
      defer.resolve(e.data);
    }, false);

    return {
        doWork : function(myData){
            defer = $q.defer();
            worker.postMessage(myData); // Send data to our worker. 
            return defer.promise;
        }
    };

}]);

js 文件夹中,我有一个文件 doWork.js,其内容是:

In the js folder I have a file doWork.js and its contents are:

self.addEventListener('message', function(e) {
  self.postMessage(e.data);
}, false);

我的 controllers.js 文件是空的,看起来像这样:

My controllers.js file is empty and it looks like so:

'use strict';

/* Controllers */
var app = angular.module("myApp.controllers",[]);

app.controller('MyCtrl1', [ '$scope', function($scope) {


}]).controller('MyCtrl2', [ '$scope', function($scope) {


}]);

我想要的是看到 Web Worker 的输出.

我在此设置中遇到的错误是:

The error I get with this setup is:

未捕获的类型错误:无法调用未定义的方法工厂"

推荐答案

你有语法错误?

改变

/* Services */
var app = angular.module('myApp.services', []).

/* Services */
var app = angular.module('myApp.services', []);

这篇关于如何在 AngularJS 中使用 Web Worker?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆