【问题标题】:running angular.js from node.js file从 node.js 文件运行 angular.js
【发布时间】:2013-08-16 19:31:29
【问题描述】:

我是 Node.js 和 anguler.js 的新手,我想使用 angular.js 将 JSON 输出显示到网页。 我的 angular.js 代码在 node.js 文件中

test.js

var pg = require('/data /node_modules/pg');
var serverAddress = localhost';
var serverPort = '1337';
var conString = "tcp://myuser:myuser@localhost/mydb";
var client = new pg.Client(conString);
http = require('http');
fs = require('fs');
url = require('url');
path = require("path");
http.createServer(function (req, res) {
console.log("request is--- "+ req.url);;
res.writeHead(200,{"Content-type":"text/html"});
getData(res);
}).listen(serverPort, serverAddress);
function getData(res) 
{
client.connect(function(err) 
{
 if(err){
  return console.error('could not connect to postgres', err);}
client.query('select * from countrydata;', function(err, result){
  if(err){
    return console.error('error running query', err);}
  res.write("<html ng-app>");
  res.write("<body> Keep it simple....!");
  res.write("<script>");
  res.write("alert('it work!');");
  res.write("</script>");
  res.write("<table ng-controller= countryController>");
  res.write("<td>{{directory}}</td>");
  res.write("</tr>");
  res.write("</table>");
  res.write("<script  
  src=\'https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js\'>");
  res.write("</script>");
  for (var i in result)
  {
     var records =result[i];
     if (i=='rows')
     {
       var json = JSON.stringify(result[i]);
     }
    }
    res.write("<script>");
    res.write("angular.controller(countryController, function($scope) {");
    res.write("$scope.directory =" + json);
    res.write("});");
    res.write("</script>");
    res.write("</body>");
    client.end();
    });

所以当我运行这个 test.js 时,我在网页上的输出看起来像这样 输出: 把事情简单化....! {{$目录}}

Html 查看页面源代码看起来列表这个--

保持简单....!{{directory}}https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js>angular.controller(countryController, function( $scope) {$scope.directory =[{"country_name":"USA","state":"Alaska"},{"country_name":"USA","state":"Colorado"},{"country_name" :"USA","state":"Florida"},{"country_name":"USA","state":"Illinois"},{"country_name":"USA","state":"Louisiana"}, {"country_name":"USA","state":"North Carolina"},{"country_name":"CHINA","state":"SHANGHAI"},{"country_name":"CHINA","state": "BEIJING"},{"country_name":"ITALY","state":"FLORENCE"},{"country_name":"ITALY","state":"PISA"},{"country_name":"INDIA", "状态":"ASSAM"}]});

我不确定为什么 Controller 不打印 $scope 的值。请让我知道如何纠正这个问题。 谢谢

【问题讨论】:

  • 您不应该使用字符串连接发送 HTML 内容。尝试将您的 HTML 页面作为静态文件提供服务。使用 JSON 将数据公开为 REST 服务,然后使用$http 服务加载数据。

标签: node.js angularjs


【解决方案1】:

我看到了以下几点:

  • 您缺少 ng-app 属性来实际引导 Angular 应用程序
  • angular.controller() 不是 API 的一部分。我认为您正在尝试使用Module.controller(),但您必须首先引用可能使用angular.module() 创建的模块
  • 在您尝试定义控制器的内联 &lt;script/&gt; 块中,您应该使用字符串文字作为控制器名称,例如'国家控制器'。您现在拥有的是一个未定义的变量......所以它的计算结果为undefined
  • 另外,&lt;table ng-controller="countryController"&gt; 标记中需要一些引号(它们不在上面)。

Soo...在让这个小示例正常工作之前,您还有一些工作要做 :-)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-22
    • 1970-01-01
    • 2020-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多