繁体   English   中英

如何使用angular获取数据并通过node.js发送数据?

[英]How get data with angular and send it with node.js?

我正在更改我的node.js应用程序,我正在使用EJS模板引擎,现在我想使用angular。

为此,我已经安装了angular并运行良好,但是现在我想获取我的数据,为此,我正在使用$ http服务:

(function($) {
  app.controller('EventCtrl', ['$scope', '$http', function($scope, $http){
    $scope.data;

    $http.get('/data').
      success(function(data, status, headers, config) {
        $scope.data = data;

      }).
      error(function(data, status, headers, config) {
        $scope.data = data;

      });

  }]);

}(jQuery));

我在后端发送数据:

  restAPI.GET(options).then(function (result) {
    res.render('data/index.html', {
      event: result,
    });
  }).then(undefined, function (error) {
    console.log(error);
  });

但是它从我正在使用控制器的同一页面返回HTML。 我在这里做错了什么?

尝试这个

$scope.data = data;

config变量应该返回以下内容:

config – {Object} –用于生成请求的配置对象。

此外,您可以查看文档以查看成功函数上所有变量的含义。

我相信您需要为渲染提供回调函数。 如:

res.render('data/index', {event: result}, function(error, theHtml) {
    console.log(error);
    res.send(theHtml);
})

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM