如何传递节点。把js服务器变量放到我的角度/html视图中?

时间:2022-04-18 19:42:10

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. I could change that third line of my server code to res.render({A:A},'view_item');

我想让它显示变量值- 5。如果我使用的是jade这样的模板引擎,那就很容易了。我可以将服务器代码的第三行更改为res.render({A:A},'view_item');

But I am using html as my template engine. 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. So how do I do this? Do I somehow pass it to the $scope and include like

但是我使用html作为模板引擎。到目前为止,我的研究告诉我,使用带有角的模板引擎通常是一个坏主意,而且总有一种方法可以使用在模板系统中构建的角。我该怎么做呢?我是否以某种方式将它传递给$scope并包含like

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

I haven't seen this done anywhere so I don't think its the way to go.

我还没见过这种方法,所以我不认为它是正确的。

2 个解决方案

#1


10  

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:
  2. 首先,您需要使用像express in node这样的库(服务器库)来设置合适的路由(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中提到的方法。

#2


4  

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. For SPAs you start out with a basic html file (probably index.html). Then, your framework handles the rendering of everything else. Your server may also emit templates, but it will never render anything itself.

如果你使用的是angle,你可能需要构建一个单页应用程序——这适用于大多数现代前端框架。对于SPAs,您将从一个基本的html文件(可能是index.html)开始。然后,框架处理所有其他内容的呈现。您的服务器也可能发出模板,但它永远不会自己呈现任何内容。

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

This shouldn't be rendering anything or returning HTML. Instead, you should be returning data that the front end will use to render -- preferably as JSON.

这不应该呈现任何东西或返回HTML。相反,您应该返回前端将用于呈现的数据——最好是JSON。

res.json({A: 5});

Then with Angular you would do something like

有角的时候你会做类似的事情

$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。得到完成任务。一个填充。

#1


10  

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:
  2. 首先,您需要使用像express in node这样的库(服务器库)来设置合适的路由(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中提到的方法。

#2


4  

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. For SPAs you start out with a basic html file (probably index.html). Then, your framework handles the rendering of everything else. Your server may also emit templates, but it will never render anything itself.

如果你使用的是angle,你可能需要构建一个单页应用程序——这适用于大多数现代前端框架。对于SPAs,您将从一个基本的html文件(可能是index.html)开始。然后,框架处理所有其他内容的呈现。您的服务器也可能发出模板,但它永远不会自己呈现任何内容。

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

This shouldn't be rendering anything or returning HTML. Instead, you should be returning data that the front end will use to render -- preferably as JSON.

这不应该呈现任何东西或返回HTML。相反,您应该返回前端将用于呈现的数据——最好是JSON。

res.json({A: 5});

Then with Angular you would do something like

有角的时候你会做类似的事情

$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。得到完成任务。一个填充。