简体   繁体   English

如何将node.js服务器变量传递到我的angular / html视图中?

[英]How do I pass node.js server variables into my angular/html view?

I have this route in my app.js file that starts the server 我在启动服务器的app.js文件中有这条路线

app.get('/view/:item_id', function(req,res){
    var A = 5;
    res.render('view_item');

and I have this in my view_item.html: 我在view_item.html中有这个:

<p>{{A}}</p>

I want it to display the variable value - 5. If I were using a template engine such as jade it would be easy. 我希望它显示变量值 - 5.如果我使用的是模板引擎,如jade,那将很容易。 I could change that third line of my server code to res.render({A:A},'view_item'); 我可以将服务器代码的第三行更改为res.render({A:A},'view_item');

But I am using html as my template engine. 但我使用html作为我的模板引擎。 My research so far has told me that using a template engine with angular is usually a bad idea, and there is always a way to do it using angular's built in template system. 到目前为止,我的研究告诉我,使用带有角度的模板引擎通常是一个坏主意,并且始终有一种方法可以使用angular的内置模板系统来完成它。 So how do I do this? 那我该怎么做? Do I somehow pass it to the $scope and include like 我以某种方式将它传递给$ scope并包含类似

<script>
    $scope.A = {{A}};
</script>

I haven't seen this done anywhere so I don't think its the way to go. 我没有在任何地方看到这样做,所以我认为它没有办法。

This is a two step process. 这是一个两步过程。

  1. First, you need to use a library(server library) like express in node to set the proper routings (REST Services) to respond to your Requests: 首先,您需要使用像节点中的 express这样的库(服务器库)来设置正确的路由(REST服务)以响应您的请求:

Server Side 服务器端

//app = express();
    app.get('/api/:paramID1/:paramID2',function(req, res){
        return res.json({ A: 5 });
    });
  1. On the client side , you need an ajax call to invoke the service like: 客户端 ,您需要ajax调用来调用服务,如:

     $http.get( "/api/1/abc").success(function( data ) { $scope.A= data; //from your sample; alert( "Load was performed. " + data ); }); 

Please note that when using REST there are different type of "methods" that can be invoked depending on your needs, such as POST, DELETE, UPDATE or the one just mentioned in the example GET. 请注意,使用REST时,可以根据需要调用不同类型的“方法”,例如POST,DELETE,UPDATE或示例GET中刚刚提到的那些。

If you are using Angular you should probably be building a single page app -- this would apply for most of the modern front end frameworks. 如果你使用的是Angular,你可能应该构建一个单页面应用程序 - 这适用于大多数现代前端框架。 For SPAs you start out with a basic html file (probably index.html ). 对于SPA,您可以从基本的html文件(可能是index.html )开始。 Then, your framework handles the rendering of everything else. 然后,您的框架处理其他所有内容的呈现。 Your server may also emit templates, but it will never render anything itself. 您的服务器也可能会发出模板,但它永远不会呈现任何内容。

app.get('/view/:item_id', function(req,res){

This shouldn't be rendering anything or returning HTML. 这不应该呈现任何内容或返回HTML。 Instead, you should be returning data that the front end will use to render -- preferably as JSON. 相反,您应该返回前端将用于呈现的数据 - 最好是JSON。

res.json({A: 5});

Then with Angular you would do something like 然后使用Angular你会做类似的事情

$http.get("/view/1").success(function (data) {
    ctrl.A = data.A;
});

Your html/template would have something like 你的html /模板会有类似的东西

<div ng-controller="ctrl as ctrl">
    <div>{{ctrl.A}}</div>

Once $http.get completes, ctrl.A is populated. $http.get完成后,将填充ctrl.A

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular:如何将$ scope变量传递到Node.js服务器。 - Angular: how to pass $scope variables into the Node.js server. 如何将数据从 HTML 表单传递到我的 Node.js 文件? - How do I pass a data from an HTML form to my Node.js file? 如何在我的本地node.js服务器html页面上包含多个库 - How do i include multiple libraries on my local node.js server html page 如何通过一个端点将数据从Angular传递到Node.Js服务器,反之亦然? - How can I pass data from Angular to Node.Js server and vice versa by one endpoint? 将变量从node.js服务器端控制器传递到angular.js客户端控制器 - Pass variables from node.js server side controller to angular.js client side controller 与使用PHP一样,如何在HTML页面中运行Node.js脚本? - How do I run a Node.js script in my HTML page, as I do with PHP? 如何从我的node.js VM实例在浏览器中查看JSON数据? - How do I view my JSON data in a browser from my node.js VM Instance? 如何将 js 文件链接到 Node.js web 服务器中的 index.html? - How do I link a js file to index.html in a Node.js web server? 如何将我的Heroku配置变量链接到Node.js应用程序 - How do I link my Heroku Config Variables to a Node.js app 如何通过node.js将数据从MongoDB传递到我的web api? - How do I pass the data from MongoDB to my web api through node.js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM