Search code examples
javascriptangularjshtmlweb-worker

using ng-webworker with application does not work


I am building an application with ng-webworker , i built a sample application with ng-webworker and added all the necessary javascript.But it does not work.

I need the function to be executed on the button click.

Code:

 <md-button ng-click="callworker()">
                    CALL WEBWORKER
 </md-button>

JS:

var routerApp =angular.module('DiginRt', ['ngMaterial','ngWebworker'])
 routerApp.controller('AppCtrl', ['$scope',function($scope,Webworker) {
function doubler(num) {
    return num * 2;
}
var myWorker = Webworker.create(doubler);
$scope.callworker = function() {
  myWorker.run($scope.value).then(function(result) {
    alert("Answer: " + result);
});
}

Here is the application


Solution

  • As per your new code link, $scope.value is not defined, you have to define it in your $scope;

    var routerApp = angular.module('DiginRt', ['ngMaterial', 'ngWebworker'])
    routerApp.controller('AppCtrl', ['$scope', 'Webworker', function ($scope, Webworker) {
        $scope.value = 10; //Added this.
        function doubler(num) {
            return num * 2;
        }
    
        var myWorker = Webworker.create(doubler);
        $scope.callworker = function () {
            myWorker.run($scope.value).then(function (result) {
                alert("Answer: " + result);
            });
        };
    }]);