$ scope变量在块外无法访问

时间:2014-10-24 12:49:11

标签: javascript angularjs

我无法在函数外使用我的$ scope变量。我在angularjs的控制器之外创建了我的工厂方法。

            myapp.factory('MessagesService', function() {
            var service = {};
            service.connect = function() {
            if(service.ws) { return; }
            var ws = new WebSocket("url");
            ws.onopen = function() {

            service.send(JSON.stringify({"msg":"lowestask"}));
            service.send(JSON.stringify({"msg":"highestbid"}));

            service.send(JSON.stringify({"msg":"top1000asks"}));
            service.send(JSON.stringify({"msg":"top1000bids"}));

            service.send(JSON.stringify({"msg":"recentbuytrades"}));
            service.send(JSON.stringify({"msg":"recentselltrades"}));

            service.send(JSON.stringify({"msg":"top10000bids"}));
            service.send(JSON.stringify({"msg":"top10000asks"}));
        };
        ws.onerror = function() {
          //  service.callback("Failed to open a connection");
            console.log("Failed to open a connection");}

        ws.onclose = function() {

            service.callback(JSON.stringify([{"msg":"Connection closed"}]));
            console.log("Connection closed");
        }
        ws.onmessage = function(message) {service.callback(message.data);};
        service.ws = ws;}

    service.send = function(message) {service.ws.send(message);}
    service.subscribe = function(callback) {service.callback = callback;}

    return service;
});

这是控制器代码,我想使用$ scope.tenasks进行图表配置

                      myapp.controller('myctrl', function              ($scope,$http,$rootScope,MessagesService) {
            $scope.nrOfRequests = 1000;
            $rootScope.websocketTotals = [];
            $rootScope.servletTotals = [];
            $scope.isLoading = false;
            $scope.status = [];
            var websocketTotal = 0;
            var servletTotal = 0;

            MessagesService.connect();

        MessagesService.subscribe(function(message) {
        console.log("msg"+message)
        $scope.$apply(function () {
        var msgobj = angular.fromJson(message);
        var msg = Object.keys(msgobj[0]);
        switch (msg[0]) {
        case "thousandbids":
        $scope.thousandbids = JSON.parse(message);
        console.log("messages are coming..")
        break;
        case "thousandasks":
        $scope.thousandasks = JSON.parse(message);
        console.log("messages2 are coming..")
        break;

        }//switch

        });

        });

               $scope.isLoading = true;
                websocketTotal = 0;
                servletTotal = 0;
                $scope.status = [];





$scope.chartConfig = {
    options: {
        chart: {
            type: 'area',
            zoomType: 'x'
        }
    },
                series: [{
                    data: $scope.websocketTotals,
                    name: "volume"
                },{
                    data: $scope.servletTotals,
                    name: "rate"
                }],
                title: {
                    text: 'Coinsecure'
                },
                xAxis: {currentMin: 0, minRange: 1},
                yAxis: {title:{text:"ms"}},
                loading: false
            }
                    });

0 个答案:

没有答案