在工厂中使用两次return语句

时间:2016-10-17 17:00:02

标签: angularjs return angularjs-service angularjs-factory

我有一个工厂代码,我需要return methods用于模板,return _this用于从控制器获取信息以通过工厂发送。   问题是我不知道如何在return选择第一个return的同时放置两者。我该如何添加? 代码:

.factory('chatroomService', function($websocket,$rootScope,$state) {
    var dataStream = $websocket('ws://localhost:9000');
    _this = this;
    var collection = ["Ola"];
    dataStream.onOpen(function() {
      _this.sendChat = function(data) {
        dataStream.send(data);
      };
    });
  var methods = {
    collection : collection,
    get: function() {
      dataStream.send(JSON.stringify({action: 'get'}));
    }
  };
    return methods; //problem here
})

1 个答案:

答案 0 :(得分:0)

你可以用两个

返回一个对象
return {methods: methods, context: _this}

要使用它,您将致电chatroomService.methodschatroomService.context