简体   繁体   English

AngularJS Socket.io:无法读取未定义的属性“ emit”

[英]AngularJS Socket.io: Cannot read property 'emit' of undefined

currently I am trying to use angularjs with node and socketio. 目前,我正在尝试将angularjs与node和socketio一起使用。 But I am getting the following error. 但是我收到以下错误。

TypeError: Cannot read property 'emit' of undefined at k.$scope.EnteredUsername ( http://localhost:1337/js/controllers.js:34:19 ) TypeError:无法读取k。$ scope.EnteredUsername( http:// localhost:1337 / js / controllers.js:34:19 )中未定义的属性'emit'

It seems like he doesnt know 'socket', when the enteredUsername()-Function is called. 当调用enterentUserName()-Function时,他似乎不知道'socket'。 Is there something wrong with my factory? 我的工厂出问题了吗?

app.js: app.js:

'use strict';

angular.module('myApp', [
  'ngRoute',
  'myApp.filters',
  'myApp.services',
  'myApp.directives',
  'myApp.controllers'
])

.config(['$routeProvider', function($routeProvider) {
  $routeProvider.when('/viewUsername', {templateUrl: 'partials/username.html', controller: 'usernameCtrl'});
  $routeProvider.when('/viewImpressum', {templateUrl: 'partials/impressum.html', controller: 'impressumCtrl'});
  $routeProvider.otherwise({redirectTo: '/viewUsername'});
}]);

controllers.js: controllers.js:

'use strict';

/* Controllers */

angular.module('myApp.controllers', [])
    .factory('socket', function($rootScope) {
        var socket = io.connect('http://localhost:1337');
        return {
            on: function(eventName, callback) {
                socket.on(eventName, function() {
                    var args = arguments;
                    $rootScope.$apply(function() {
                        callback.apply(socket, args);
                    });
                });
            },
            emit: function(eventName, data, callback) {
                socket.emit(eventName, data, function() {
                    var args = arguments;
                    $rootScope.$apply(function() {
                        if(callback) {
                            callback.apply(socket, args);
                        }
                    });
                });
            }
        };
    })
    .controller('usernameCtrl', ['$scope', function($scope, socket) {
        $scope.username = 'guest';

        $scope.EnteredUsername = function() {
            var username = $scope.username;
            socket.emit('enteredUsername', username);
        }
    }]);

您错过了将其添加为字符串的情况:

.controller('usernameCtrl', ['$scope', 'socket', function($scope, socket) {

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

相关问题 Socket.io:“无法读取未定义的属性'emit'” - Socket.io : “Cannot read property 'emit' of undefined” Socket.IO TypeError:无法读取未定义的属性“发出” - Socket.IO TypeError: Cannot read property 'emit' of undefined Socket.io - UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'emit' of undefined - Socket.io - UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'emit' of undefined WebRTC,socket.io,node.js:无法读取未定义的属性'emit'? - WebRTC , socket.io, node.js: Cannot read property 'emit' of undefined? 使用React / Socket.IO无法读取未定义错误的属性'emit' - Cannot read property 'emit' of undefined error using React/Socket.IO Karma无法读取未定义的socket.io的属性'prototype'? - Karma Cannot read property 'prototype' of undefined socket.io? Node.js和Socket.io-无法读取未定义的属性“ on” - Node.js & Socket.io - Cannot read property 'on' of undefined Socket.io 未捕获类型错误:无法读取未定义的属性“应用” - Socket.io Uncaught TypeError: Cannot read property 'apply' of undefined Socket.io TypeError:无法读取未定义的属性“hteam” - Socket.io TypeError: Cannot read property 'hteam' of undefined Socket.io引发“ TypeError:无法读取未定义的属性'push'” - Socket.io raises “TypeError: Cannot read property 'push' of undefined”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM