如何从Node.js的服务器功能传递到客户端的 [英] How to pass function to client from Node.js server

查看:207
本文介绍了如何从Node.js的服务器功能传递到客户端的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有

我想要做的是这样的:

在节点服务器端:

var fn = function(){
    alert("hello");
}

我要发送此功能,客户端(目前使用AngularJS,但它并不重要,只要这个问题是可以解决的),并将其绑定到一个按钮单击事件。这样我就可以弹出警告窗口单击按钮时。

I want to send this function to client side(currently using AngularJS, but it does not matter as long as this problem can be solved), and bind it to a button click event. So I can get pop-out alert window when click that button.

感谢

推荐答案

所以,这样的事情:

// Predefined functions
var allowedFunctions = {
    'f1': function () {alert('Called f1');},
    'f2': function () {alert('Called f2');},
    'f3': function () {alert('Called f3');},
};

// This comes from the server
var callThisOne = 'f2';

// Here's how you call it now
allowedFunctions[callThisOne]();

这篇关于如何从Node.js的服务器功能传递到客户端的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆