【问题标题】:Get array data from angularjs从 angularjs 获取数组数据
【发布时间】:2015-11-14 04:10:30
【问题描述】:

首先,对不起我的英语。我想知道如何从 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\"}]}"

【问题讨论】:

  • @adamkwadsworth 我猜 plnkr 上没有 nodejs :(
  • 你的服务器端路由和控制器在哪里?
  • @evc 你的意思是nodejs的服务器端脚本吗?我有它,但我认为你只需要写文件的一部分。 angularjs的控制器?我已经包含在我的问题表中
  • 您首先需要了解服务器端代码和客户端代码之间的区别。然后你会意识到var nama = document.getElementById("namaFile").value 在你的 NodeJS 代码中毫无意义。
  • @dfsq 但我确实将数组保存为 json 格式(请检查更新问题)

标签: javascript arrays angularjs node.js


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2014-05-10
    • 1970-01-01
    • 2015-02-05
    • 2015-04-04
    • 1970-01-01
    • 2017-12-30
    • 1970-01-01
    • 2013-11-22
    • 1970-01-01
    相关资源
    最近更新 更多