将数组从控制器传递到出厂错误

时间:2016-10-15 15:33:20

标签: angularjs angularjs-service angularjs-controller angularjs-factory

我有一个工厂,使用angular-websocket连接后端,我需要将信息从我的控制器传递给它,以便它发送该信息,我想将其作为一个数组传递。 工厂就是这样:

.factory('registerService', function($websocket) {
  var dataStream = $websocket('ws://localhost:3000'); //websocket
  dataStream.onOpen(function() {
    console.log('in');
    this.sendId = function(data) {
    console.log('data: '+data);
    };
  })
  dataStream.send(function(message) {
    //send info to backend
  });
})

和控制器一样:

.controller("registerController", function($scope,$stateParams,$ionicPopup,$state,registerService) {
  $scope.registerService = registerService;
  $scope.signUp = function() {
        registerBox = [];
        registerBox.push('Register')
        registerService.sendId(registerBox);
  };
})

但是我收到错误:registerService.sendId不是函数。 问题是什么?

1 个答案:

答案 0 :(得分:0)

     .factory('registerService', function($websocket) {
          var dataStream = $websocket('ws://localhost:3000'),
              _this = this;

          dataStream.onOpen(function() {
            console.log('in');
            _this.sendId = function(data) {
               console.log('data: '+data);
            };
          })
          dataStream.send(function(message) {
            //send info to backend
          });

         return _this;
   });