如何使用AngularJS网络工作者?如何使用、工作者、网络、AngularJS

2023-09-13 04:38:14 作者:听风看月

我使用 AngularJS种子,然后我希望看到一个Web工作者的工作落实。

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

我想,以了解它做一个简单的Web工人的工作,但我正在与功能性的问题。

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

我在Web工作code在 services.js 像这样:

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) {


}]);

我要的是看到了网络工作者的输出。

我这种设置得到的错误是:

The error I get with this setup is:

的未捕获类型错误:不能调用方法'工厂'未定义的

推荐答案

您有语法错误?

变更

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

/* Services */
var app = angular.module('myApp.services', []);
 
精彩推荐
图片推荐