从angularjs获取数组数据

时间:2015-08-20 13:27:12

标签: javascript arrays angularjs node.js

首先,对不起我的英语。我想知道如何从angularjs获取数组数据,所以我可以用nodejs保存它。

这是我的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);
                }
            });

这是我的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!");
            }
        }); 
    }

我认为“myJson”应该来自angularjs数组,即“addList.lists = [];”但我不知道该怎么做。或者也许有另一种方式?

- 编辑 -
我认为唯一的解决方案是将数组保存到localStorage并将其保存为json格式。但我有另一个问题,它取代所有空格到这个字符“\”它很烦人。

下面是以下代码(添加一些更改),假设我们已经将数组存储到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]);});

如果我运行此代码,它会给我一个很好的输出

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

当我尝试这个时

 console.log(data[2]);

它给我“\”作为输出,顺便说一下这里是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\"}]}"

1 个答案:

答案 0 :(得分:0)

向你的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
                }
            });

然后你必须拥有发布类型的请求的路由,然后在执行此路由请求的控制器中,你必须执行文件保存操作