如何从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]();