在工厂中使用 return 语句两次
Use return statement twice in 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
})
您可以 return 一个同时具有两者的对象
return {methods: methods, context: _this}
要使用它,您需要调用 chatroomService.methods
或 chatroomService.context
我有一个工厂代码,我需要 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
})
您可以 return 一个同时具有两者的对象
return {methods: methods, context: _this}
要使用它,您需要调用 chatroomService.methods
或 chatroomService.context