[英]Launch new app from app in new tab Angular-Node
I have an Angular 1.x - Node application. 我有一个Angular 1.x-Node应用程序。 I need to launch another alike application from the first one, in a new tab, using Google Chrome.
我需要使用Google Chrome从第一个应用程序的新选项卡中启动另一个类似的应用程序。 The process flow is the following:
处理流程如下:
- user starts from a page listing a number of applications available. -用户从列出许多可用应用程序的页面开始。
- user clicks on a Run button, defined as here (is inside a table row): -用户单击“运行”按钮,定义如下(位于表行中):
<td><button type="button" class="btn btn-primary" ng-click="vm.onSubmit({'operation': 'run', 'slug': application.slug, 'port': application.port});">Run</button></td>
- the onSubmit click handler, from the view's Angular controller, sends the JSON data, via an http POST request, to the appropriate Node controller. -视图的Angular控制器中的onSubmit单击处理程序,通过http POST请求将JSON数据发送到相应的Node控制器。
- the Node controller starts the server's application as following: -节点控制器按以下方式启动服务器的应用程序:
console.log("Application run request on the server side, port: " + req.body.port + " slug: " + req.body.slug);
var fork = require('child_process').fork;
var child = fork('../../' + req.body.slug);
res.status(200).json({ "port": req.body.port });
- the .success method of the http POST request in the Angular controller picks up the application-to-open-in-new-tab port -Angular控制器中的HTTP POST请求的.success方法获取了应用程序在新标签页中打开的应用程序端口
return $http.post('/api/applications', data)
.success(function (result) {
console.log("port is " + result.port);
// What should I do here (and elsewhere)?
})
// catch the error message
.error(function (error) {
vm.error = error;
});
and ... here I need your help! 并且...在这里我需要您的帮助!
All the process flow works fine up to this point, the new application instance is launched in Node (the above console message is displayed, and, if I open manually a new tab and make http://localhost:port_nb , I'm getting what I need to be display-triggered automatically from the first application). 至此,所有流程都可以正常工作,在Node中启动了新的应用程序实例(显示了上面的控制台消息,并且,如果我手动打开一个新标签并创建http:// localhost:port_nb ,则说明我需要从第一个应用程序自动触发显示的内容)。 My question is: where in the Angular space (view-controller) and how should I implement something close to
我的问题是:在Angular空间中的哪个位置(视图控制器),我应该如何实现接近
$window.open('http://localhost:port_nb', '_blank');
knowing that this would work on a direct button click, which is not my case. 知道这将在直接单击按钮时起作用,而不是我的情况。 A formal answer would be perhaps "create a click event", but that doesn't come up into my mind how.
正式的答案可能是“创建点击事件”,但是我没有想到如何。 Thanks!
谢谢!
You should implemenet onSubmit
method in Angular's controller as follows 您应该按以下方式在Angular的控制器中实现
onSubmit
方法
vm.onSumbit = function(data) {
return $http.post('/api/applications', data)
.success(function (result) {
console.log("port is " + result.port);
$window.open('http://localhost:' + result.port, '_blank');
})
// catch the error message
.error(function (error) {
vm.error = error;
});
}
Also make sure to reference the controller Controller as vm
. 还要确保将控制器
Controller as vm
引用Controller as vm
。
It is best to store $http
requests in a factory or a service. 最好将
$http
请求存储在工厂或服务中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.