简体   繁体   English

当其中存在socket.io事件时,Angularjs控制器不起作用

[英]Angularjs controller doesn't work when socket.io event is in there

I have a socket.io event being initialized on connection that is supposed to change value of items inside this controller (it controls chart). 我在连接上初始化了一个socket.io事件,该事件应该更改此控制器(控制图表)中项目的值。 But when there is mySocket.on it doesn't work at all, don't render anything in this controller. 但是当有mySocket.on时,它根本不起作用,请不要在此控制器中呈现任何内容。 When I take it out it works just fine. 当我将其取出时,效果很好。 mySocket is a proper function, I'm using it in other controllers without any problems. mySocket是一个适当的功能,我正在其他控制器中使用它,没有任何问题。

.controller('KnobCtrl', ['$scope', function($scope, mySocket) {
        $scope.itemsCounter = 0;
        $scope.roundValue = 0;

        mySocket.on('newConnectionCounter', function (itemsInRoundCounter, valueOfRound) {
            $scope.itemsCounter = itemsInRoundCounter;
            $scope.roundValue = valueOfRound;
        });

        $scope.data = {
            value: $scope.itemsCounter,
            options: {
            width: 190,
            fgColor: "#FFAB40",
            skin: "tron",
            thickness: .3,
            displayPrevious: false,
            readOnly: true,
            max: 30,
                inputColor: "#ffffff"
        }
        };
        $scope.options = {
            width: 150,
            fgColor: "#ffec03",
            skin: "tron",
            thickness: .8,
            displayPrevious: false,
            readOnly: true,
            max: 50
        };

        $scope.formatOptions = function(data) {
            data.formattedOptions = JSON.stringify(data.options).replace(/,/g,"\n");
            return data;
        };
    }]);

It looks like you are expecting it to be injected into your controller function, but you are missing the string in the array passed to the .controller method: 您似乎希望将其注入到控制器函数中,但是缺少传递给.controller方法的数组中的字符串:

.controller('KnobCtrl', ['$scope', function($scope, mySocket) {
    ...
 });

Any arguments to that function should be represented in the array: 该函数的任何参数都应在数组中表示:

 .controller('KnobCtrl', ['$scope', 'mySocket', function($scope, mySocket) {
    ...
 });

This assumes that "mySocket" is something that exists in the "Angular world" (created with angular.service, angular.factory, etc.). 假设“ mySocket”是“ Angular world”中存在的东西(使用angular.service,angular.factory等创建)。 If "mySocket" is just some globally defined function, then you don't want it listed as an argument. 如果“ mySocket”只是一些全局定义的函数,则您不希望将其列为参数。 If nothing is passed in that argument, it would be undefined even if it exists globally. 如果未在该参数中传递任何内容,则即使该参数全局存在,也将是不确定的。 Note: if it is just a globally defined object outside of Angular, you will likely need to call $scope.$apply() to kick of a digest cycle in your handler. 注意:如果它只是Angular之外的全局定义对象,则可能需要调用$ scope。$ apply()来启动处理程序中的摘要循环。

Take a look at the places where it is working in your app and see how you injected it into your controller. 看一看它在您的应用程序中的工作位置,看看如何将其注入到控制器中。

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

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