繁体   English   中英

将数据从服务传递到 controller AngularJS 1.3.15

[英]Pass data from service to controller AngularJS 1.3.15

从startingWebGazer 服务向我的WorkspaceController.js 发送数据时遇到了一个大问题。 我有一个 webgazer.js,它评估预测点并将其传递给 StartingWebgazer.js 中的 xprediction 和 yprediction 变量(这是我的服务)

app.service('startingWebGazer', function () {
this.startgazer = function () {
    window.onload = function () {
        webgazer.setRegression('ridge') /* currently must set regression and tracker */
            .setTracker('clmtrackr')
            .setGazeListener(function (data, elapsedTime) {
                if (data == null) {
                    return
                }
                var xprediction = data.x; //these x coordinates are relative to the viewport
                console.log(xprediction);
                return xprediction;
                // var yprediction = data.y; //these y coordinates are relative to the viewport
            }).begin()
            .showPredictionPoints(true); /* shows a square every 100 milliseconds where current prediction is */
        var setup = function () {

            //Set up the main canvas. The main canvas is used to calibrate the webgazer.
            var canvas = document.getElementById("plotting_canvas");
            canvas.width = window.innerWidth;
            canvas.height = window.innerHeight;
            canvas.style.position = 'fixed';
        };

        function checkIfReady() {
            if (webgazer.isReady()) {
                setup();
            } else {
                setTimeout(checkIfReady, 100);
            }
        }

        setTimeout(checkIfReady, 100);

    };


    window.onbeforeunload = function () {
        console.log('user want to left the page');
        //webgazer.end(); //Uncomment if you want to save the data even if you reload the page.
        window.localStorage.clear(); //Comment out if you want to save data across different sessions
    }
}});

所以,我想将 xprediction 传递给 WorkspaceController.js,以便将来在 controller 中进行一些评估

app.controller('WorkspaceController', ['$scope', 'startingWebGazer', function ($scope, startingWebGazer) {
// create configuration object
$scope.startingWebGazer = startingWebGazer;
console.log($scope.startingWebGazer);
console.log('data in workspace', $scope.startingWebGazer.startgazer());}]);

但我有一个大问题,因为 startgazer 是未定义的,尽管 xprericction 评估良好(这是 console.log 显示的) 在此处输入图像描述

什么是问题? 为什么我可以在 console.log 中看到 xprediction,但不能将其传递给 controller? 我应该怎么做?

我不确定我是否正确理解了您的问题,但startgazer不会按照定义的方式返回值。 它分配window.onloadwindow.onbeforeunload然后在没有返回语句的情况下完成,这意味着它返回undefined 您可以考虑将xprediction分配给服务 scope 中的变量并返回该变量。 我可以想象这样的事情可以达到你的目的:

app.service('startingWebGazer', function () {
    let x = 0;

    this.startgazer = function () {
        window.onload = function () {
            ...
            x = xprediction;
            ...
        }
        ...
    }

    this.getXPrediction = function() {
        return x;
    }
})

我认为进一步重构代码并例如在运行方法中调用startgazer或使getXPrediction返回 promise 是有意义的,但上面的示例可以帮助您入门。 希望能帮助到你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM