从Angular Controller调用节点函数的常用方法 [英] Common method for calling a node function from Angular Controller

查看:99
本文介绍了从Angular Controller调用节点函数的常用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我在此开头加上我是Node/Express的新手.

Let me prefix this with that I am newer to Node/Express.

我有一个AngularJS应用程序,该应用程序利用Node.JS来管理Azure Blob要求,例如如下创建Blob容器:

I have an AngularJS App that is leveraging Node.JS for managing Azure Blob requirements such as creating Blob containers as follows:

function test(containerName) {
blobSvc.createContainerIfNotExists(containerName, function (error, result, response) {
    if (!error) {
// Container exists and allows
// anonymous read access to blob
// content and metadata within this container
    }
});
};

test('blob4');

从Node中的server.js执行时创建容器的功能按预期工作,并创建了一个blob容器.但是,我需要在AngularJS应用中单击创建一个Blob容器.我设想使用导出来访问和执行在Server.js中创建的函数,但是已经看到了一些混合信息,尤其是当Express.js在图片中时,通过AngularJS客户端调用Node.js函数,因为这似乎是在Angular App中必须进行一次http调用(请参阅本文的最后答案:从角度应用程序调用nodejs中的函数).

The function for creating a container when executed from server.js in Node works as expected and creates a blob container. However, I need to create a blob container on click in my AngularJS app. I envisioned using exports to access and execute functions created in Server.js but have seen some mixed information, especially when Express.js is in the picture, for calling a Node.js function via AngularJS client side since it appears that in an Angular App an http call would have to be made (please see the last answer in this post: Call function in nodejs from angular application).

我的问题如下:

1)由于我的应用程序当前使用Node,Express和Angular,因此我是否需要在Angular控制器中使用http来运行Node函数/执行用Node/Server.js编写的所有函数,如果调用则需要$ http来执行通过AngularJS客户端,即使他们不调用服务,但可能是执行诸如数学之类的功能的函数?基于Express的呼叫示例:

1) Since my app currently uses Node, Express, and Angular, would I need to use the http in my Angular controller to run Node functions/do all functions written in Node/Server.js require $http to execute if called via AngularJS client side even if they don't call a service but might be a function performing something such as math? Example of an Express based call:

function MyCtrl($scope, $http) { 
// $http is injected by angular's IOC implementation

// other functions and controller stuff is here...    

// this is called when button is clicked
$scope.batchfile = function() {
    $http.get('/performbatch').success(function() {
        // url was called successfully, do something 
        // maybe indicate in the UI that the batch file is
        // executed...
    });
}
}

2)或者正在使用导出(如本帖子中列出的更常见的做法),其中将函数定义为导出,然后通过需求导入:

2) Or is using exports, such as listed in this post more common practice, where the function is defined as an export and then imported via a requires: What is the purpose of Node.js module.exports and how do you use it?. If so, would I do something like the following?:

Node Server.JS文件:

Node Server.JS File:

var myFunc1 = function() { ... };
exports.myFunc1 = myFunc1;

在AngularJS控制器内(不包括作为依赖项):

Within an AngularJS Controller (not including as a dependency):

var m = require('pathto/server.js');
m.myFunc1();

3)最后,我是否完全脱离基础,并且有一种常见的做法是从缺少的Angular控制器中调用node.js函数?

3) Lastly, am I completely off base and there is a common practice for calling node.js functions from an Angular Controller that I am missing?

推荐答案

首先,虽然nodejs和angularjs都是JavaScript,但两者都是两个不同的实现.

First of all nodejs and angularjs all though both are javascript both are two different implementation.

NodeJS可在服务器上运行,另一方面angularjs可在浏览器上运行.

NodeJS works on server, on other hand angularjs works on browser.

最初,当我是节点的新手时,我也遇到了同样的问题.我以为我们可以直接从angularjs调用node函数,毕竟一切都对javascript正确!但是我错了..

initially when i was newbie to node i was also having the same problem . i was thinking we could directly call the node function from angularjs ,after all everything is javascript right ! but i was wrong..

现在在这里您应该怎么做

首先在nodejs中创建一条路由(没什么,只需创建一个简单的restAPI)

First create a route in nodejs (its nothing,just create a simple restAPI)

app = express();

app.get('/dowork',function(res,req){
    console.log(req.params.msg);
  /... code to do your work .../
});

现在在angularjs调用中可以正常工作

now in angularjs call do work

$http.get('http://localhost:8080/dowork',{"msg":"hi"}).success(function(data){
 console.log(data);
});

不确定是否会req.params.msg,但是您可以登录req并找到对象.

Im not sure will it req.params.msg but you can log req and can find the object.

在发布请求的情况下,您的参数将在req.body

In case of post request your parameters will be in req.body

这篇关于从Angular Controller调用节点函数的常用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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