Javascript AngularJS:Can';无法访问回调函数内的作用域

Javascript AngularJS:Can';无法访问回调函数内的作用域,javascript,angularjs,scope,Javascript,Angularjs,Scope,正在从指令链接函数调用下面的函数。我的问题是,我无法访问然后函数中的作用域 function workerInit(scope) { var data = scope.vm.graphData; graphViewService.send(new WorkerData(data.node[0].id,2,data.node[0].currentEntity)).then(function(response){ postWorkerRespo

正在从指令链接函数调用下面的函数。我的问题是,我无法访问然后函数中的作用域

function workerInit(scope) {
        var data = scope.vm.graphData;
        graphViewService.send(new WorkerData(data.node[0].id,2,data.node[0].currentEntity)).then(function(response){
            postWorkerResponse(response);
            //scope is not defined
        })

        nodesinQueue.push(data.node[0].id)
    }
我的工厂代码

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

    var graphViewService = {};
    var worker = new Worker('./app/modules/common/graphViewWorker.js');
    var defer = $q.defer();
    worker.addEventListener('message', function(e) {
    defer.resolve(e.data);
    }, false);

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

}])
我的指令代码(从原始代码中删除了不必要的函数def)


有人能帮我吗?

通过添加应用功能进行检查

function workerInit(scope) {
    var data = scope.vm.graphData;
    graphViewService.send(new WorkerData(data.node[0].id,2,data.node[0].currentEntity)).then(function(response){
        scope.$apply(function () {
          postWorkerResponse(response);
          //Check here able to access scope  
        });
    });
    nodesinQueue.push(data.node[0].id)
}

“你能多分享一点你的指令吗?”尼古拉·德拉姆森补充道。你能看一下吗嗯,我看不出有什么理由不应该在
然后
回调中定义它,因为它应该在关闭状态下被捕获。调用
workerInit
时是否定义了范围?你能尝试将作用域分配给then函数之外的新变量并使用它吗?@Nikolajdallarsen不走运!。。我尝试添加
var newScope=scope
,并尝试访问
newScope
内部,然后回拨。但它不起作用。我的要求很简单,我只需要将工作线程返回的值分配给角度范围。你能给我提点别的建议吗?一开始很难做到。:-)我唯一的另一个想法是将
workerInit
函数(以及任何相关函数)移动到
linkFunction
主体中,并直接使用作用域(而不是通过函数参数传递)。但是,这是一个很长的机会,因为我不确定它为什么不能像预期的那样工作。Veera,我没有权限访问示波器本身。因此,我无法访问未定义的$apply
function workerInit(scope) {
    var data = scope.vm.graphData;
    graphViewService.send(new WorkerData(data.node[0].id,2,data.node[0].currentEntity)).then(function(response){
        scope.$apply(function () {
          postWorkerResponse(response);
          //Check here able to access scope  
        });
    });
    nodesinQueue.push(data.node[0].id)
}