简体   繁体   English

从angularjs获取数组数据

[英]Get array data from angularjs

First of all, sorry for my English. 首先,对不起我的英语。 I'm wondering how to get an array data from angularjs, so i can save it with nodejs. 我想知道如何从angularjs获取数组数据,以便可以使用nodejs保存它。

Here is my angularjs script: 这是我的angularjs脚本:

        angular.module('myAddList', [])
            .controller('myAddListController', function(){
                var addList = this;
                addList.lists = [];  

                addList.tambah = function(){
                    addList.lists.push({title:addList.listTitle,greet:addList.listGreet});
                    addList.listTitle = '', addList.listGreet = '';
                }

                addList.hapusList = function(list){
                    addList.lists.splice(addList.lists.indexOf(list), 1);
                }
            });

and here is my nodejs: 这是我的nodejs:

    var fs = require("fs");

    var d = new Date();

    var myJson = {title : {
          "lists": []
        }
    };

    function saveFile(){
        fs.writeFile( document.getElementById("namafile").value + ".json", JSON.stringify( myJson ), "utf8", function(err) {
            if(err) {
                return console.log(err);
            }else if(!err){
                console.log("The file was saved!");
            }
        }); 
    }

I think "myJson" should be from angularjs array which is "addList.lists = [];" 我认为“ myJson”应来自“ addList.lists = [];”的angularjs数组。 but i dont know how to do that. 但我不知道该怎么做。 Or maybe there is an alternative way? 也许还有另一种方法?

-- Edit -- -编辑-
I think the only solution is to save the array to localStorage and save it to json format. 我认为唯一的解决方案是将数组保存到localStorage并将其保存为json格式。 But i have another problem it replace all whitespaces to this character "\\" it so annoying. 但是我还有另一个问题,那就是将所有空格替换为该字符“ \\”,这很烦人。

Here is the following code (add a few changes), let's assume we already stored array to localStorage and save it using nodejs: 这是下面的代码(添加一些更改),假设我们已经将数组存储到localStorage并使用nodejs进行保存:

var fs = require("fs");
var myJson = {
    key: "myvalue"
};

var d = new Date();
var locS = localStorage.getItem("invoice");

function saveFile(){
    var nama = document.getElementById("namaFile").value;
    fs.writeFile( nama + ".json", JSON.stringify( locS ), "utf8", function(err) {
        if(err) {
            return console.log(err);
        }else if(!err){
            console.log("The file was saved!");
        }
    }); 
}
myJson = fs.readFile("undefined.json", "utf8", function (err,data) {
          if (err) {
            return console.log(err);
          }
          console.log(JSON.parse(data));
          console.log(data[2]);});

if i run this code, it give me a nice output 如果我运行此代码,它会给我一个不错的输出

 console.log(JSON.parse(data));

and when i tried this 当我尝试这个

 console.log(data[2]);

it give me "\\" as an output, btw here is the json file 它给我“ \\”作为输出,顺便说一下,这里是json文件

"{\"tax\":13,\"invoice_number\":10,\"customer_info\":{\"name\":\"Mr. John Doe\",\"web_link\":\"John Doe Designs Inc.\",\"address1\":\"1 Infinite Loop\",\"address2\":\"Cupertino, California, US\",\"postal\":\"90210\"},\"company_info\":{\"name\":\"Metaware Labs\",\"web_link\":\"www.metawarelabs.com\",\"address1\":\"123 Yonge Street\",\"address2\":\"Toronto, ON, Canada\",\"postal\":\"M5S 1B6\"},\"items\":[{\"qty\":10,\"description\":\"Gadget\",\"cost\":9.95,\"$$hashKey\":\"004\"}]}"

Make $http request to your nodejs server like that 像这样向您的nodejs服务器发出$ http请求

 angular.module('myAddList', [])
            .controller('myAddListController', function($http){//inject $http
                var addList = this;
                addList.lists = [];  

                addList.tambah = function(){
                    addList.lists.push({title:addList.listTitle,greet:addList.listGreet});
                    addList.listTitle = '', addList.listGreet = '';
                }

                addList.hapusList = function(list){
                    addList.lists.splice(addList.lists.indexOf(list), 1);
                }
                $http.post('your server url',addList).success(function(successReponse){
                     //do stuff with response
                }, function(errorResponse){  
                    //do stuff with error repsonse
                }
            });

and then you must have route for that request with post type, and then in controller that performs this route request you must perform your file save operations 然后必须具有该请求类型的请求的路由,然后在执行该路由请求的控制器中必须执行文件保存操作

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

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