Javascript 如何通过Angular.js和Node.js服务建立连接?

Javascript 如何通过Angular.js和Node.js服务建立连接?,javascript,json,angularjs,node.js,express,Javascript,Json,Angularjs,Node.js,Express,我有一个简单的管理面板,里面有表单。我正在尝试通过Angular.js和Node.js连接更新此表单的内容。如果我做到了这一点:我将能够读取表单的数据并在首页上呈现 此表单从以下位置读取数据:“/json/form data.json”: [{ "name" : "BigTitleLine1", "content" : "APP TITLE 1" }, { "name" : "BigTitleLine2", "content" : "APP TITLE 2" }];

我有一个简单的管理面板,里面有表单。我正在尝试通过Angular.js和Node.js连接更新此表单的内容。如果我做到了这一点:我将能够读取表单的数据并在首页上呈现

此表单从以下位置读取数据:“/json/form data.json”

[{
  "name"    : "BigTitleLine1",
  "content" : "APP TITLE 1"
}, {
  "name"    : "BigTitleLine2",
  "content" : "APP TITLE 2"
}];
Angular服务从该json文件获取数据:

ServiceModule.factory('serviceFormData', ['$resource', function ($resource) {
    return $resource(rootPath("/json/form-data.json"), {}, {
        query: {
            method: 'GET',
            isArray: true
        }
    });
}]);
角度控制器将数据分配给模型:

ctrl.controller('formCtrl', ['$scope', '$rootScope', 'serviceFormData',
    function($scope, $rootScope, serviceFormData) {

        $scope.model = {};
        serviceFormData.query(function(data) {
            $scope.model.formData = data;
        });
]);
我可以在HTML上显示真实数据,并保存对模型的更改:

<form>
      <div class="form-group" ng-repeat="item in model.formData">
          <label>- {{ item.name }}</label>
          <input class="form-control" ng-model="item.content" required />
          <br />
      </div>
</form>
我正在尝试编写一个json文件,其中包含提交的angular form模型。如下所示:

[{
  "name"    : "BigTitleLine1",
  "content" : "I've edited this json from my angular form which is on the client side."
}, {
  "name"    : "BigTitleLine2",
  "content" : "APP TITLE 2"
}];

如何在Angular.js和Node.js之间建立此连接?

您应该更改工厂以处理对Node的新调用以更新文件(或创建新工厂):

然后,应使用更新方法更新角度控制器:

var json = new serviceFormData.update($scope.item);

json.$update(function (response) {
    $scope.success = true;
}, function (response) {
    $scope.error = response;
});
最后,您应该使用新路由按节点进行处理:

var bodyParser = require('body-parser');

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.put('/update', function (req, res) {
  var arr = []
    for (var index in req.body){
      arr.push(req.body[index])
    }
    var jsonData = JSON.stringify(arr, null, 2);

    fs.writeFile("./json/test.json", jsonData, function(err) {
       if(err) {
          return console.log(err);
       }
       res.json({ success: true });
    });
});

我没有测试它,但这是一条路。

非常感谢您的回答,+1。我已经在angular和node.js之间建立了连接,但我无法正确写入数组数据,我接受您的答案。再次感谢.mmm,如果您使用console.log将数据发送到节点之前,它是一个数组还是一个对象?它是一个数组,与原始数组一样。检查此问题:请将工厂更改为:
update:{method:'PUT',isArray:true}
var json = new serviceFormData.update($scope.item);

json.$update(function (response) {
    $scope.success = true;
}, function (response) {
    $scope.error = response;
});
var bodyParser = require('body-parser');

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.put('/update', function (req, res) {
  var arr = []
    for (var index in req.body){
      arr.push(req.body[index])
    }
    var jsonData = JSON.stringify(arr, null, 2);

    fs.writeFile("./json/test.json", jsonData, function(err) {
       if(err) {
          return console.log(err);
       }
       res.json({ success: true });
    });
});