簡體   English   中英

AngularJS:無法訪問回調函數內部的范圍

[英]AngularJS : Can't able to access scope inside call back function

從指令鏈接功能中調用以下功能。 我的問題是,我無法訪問然后范圍內的功能

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 () {
    var app = angular.module('sureApp');
    app.directive('graphView', function ($rootScope, graphViewService) {

        var controller = function () {
            var vm = this;
            //controller code
        };

        var linkFunction = function (scope, elem, attrs) {
            var el = angular.element(elem);
            var graph = graphInit(scope, scope.vm.graphData.node[0]);
            nodes = new vis.DataSet(graph.nodeList);
            edges = new vis.DataSet(graph.edgeList);
            var container = el.find('#network')[0];
            var data = {
                nodes: nodes,
                edges: edges
            };
            network = new vis.Network(container, data, networkOptions());
            networkEvents(network, scope);
            new detailWindow(el, scope);
        }        

        function graphInit(scope, node) {
            var nodeList = [];
            var edgeList = [];
            workerInit(scope);
            scope.vm.graphChips = [];
            scope.vm.graphChips.push(node.currentEntity);
            nodeList.push(updateNodeStructure(node, lIcons));
            return {
                nodeList: nodeList,
                edgeList: edgeList
            }
        }

        function workerInit(scope) {
            var data = scope.vm.graphData;
            WorkerData.url = data.url;
            WorkerData.requestHeaders = requestConfig.headers;
            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)
            console.time("test");
        }

        function WorkerData(id, level, currentEntity){
            this.url = WorkerData.url
            this.requestHeaders = WorkerData.requestHeaders;
            this.id = id;
            this.level = level;
            this.currentEntity = currentEntity;
        }

        //removed other function def

        return {
            restrict: 'E',
            scope: {
                graphData: '=',
                detailedView: '=',
                mainEntity: '='
            },
            link: linkFunction,
            controller: controller,
            controllerAs: 'vm',
            bindToController: true,
            templateUrl: './app/modules/common/graphView.html'
        };
    })
}());

誰可以幫我這個事?

通過添加應用功能進行檢查

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM