36174

How to pass function to client from Node.js server

Question:

All:

What I want to do is like:

<strong>On Node server side:</strong>

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

I want to <strong>send this function to client side</strong>(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.

Thanks

Answer1:

So, something like this:

// 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]();

Answer2:

Here's a <a href="http://plnkr.co/edit/WSJJIA2CWxGXCXCMvfRA?p=preview" rel="nofollow">plunk</a>.

// Get this from the server var textReceived = 'var fn = function(){ alert("hello"); };'; function getFunction(textReceived) { eval(textReceived); return fn; } var f = getFunction(textReceived); f();

Recommend

  • angular Scroll div on button click
  • Getting webGL error in autodesk viewer
  • How to re-render views using Angular JS
  • Installing SSL on AWS EC2 Bitnami Mean Stack
  • Typecasting `this` of a base class template to its derived class
  • how do you obtain the address of an instance after overriding the __str__ method in python
  • Java : How to tint this PNG programmatically?
  • HttpListener.IsSupported is false on XP SP3
  • Knockout custom binding handler
  • Ember.js model to be organised as a tree structure
  • Inline R code in YAML for rmarkdown doesn't run
  • jQuery ready not fired after rails link_to is clicked
  • onBackPressed() not being executed
  • Jackson Parser: ignore deserializing for type mismatch
  • d3 v4 drag and drop with TypeScript
  • Content-Length header not returned from Pylons response
  • JQuery Internet Explorer and ajaxstop
  • Play WS (2.2.1): post/put large request
  • How to clear text inside text field when radio button is select
  • OpenGL ES texture problem, 4 duplicate columns and horizontal lines (Android)
  • Android fill_parent issue
  • How to access EntityManager inside Entity class in EJB3
  • Why ng-show works with ng-repeat but ng-if doesn't? [duplicate]
  • jQuery show() function is not executed in Safari if submit handler returns true
  • Opengl-es onTouchEvents problem or a draw problem? [closed]
  • How would I use PHP exceptions to define a redirect?
  • Join two tables and save into third-sql
  • vba code to select only visible cells in specific column except heading
  • How to model a transition system with SPIN
  • ORA-29908: missing primary invocation for ancillary operator
  • jquery mobile loadPage not working
  • How can I estimate amount of memory left with calling System.gc()?
  • Properly structure and highlight a GtkPopoverMenu using PyGObject
  • Data Validation Drop Down Box Arrow Disappearing
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • How do you join a server to an Active Directory (domain)?
  • How to get Windows thread pool to call class member function?
  • Linking SubReports Without LinkChild/LinkMaster
  • Is there any way to bind data to data.frame by some index?
  • Converting MP3 duration time