Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

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.

Here is my angularjs script:

        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:

    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 = [];" 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. 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:

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

"{\"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\"}]}"
share|improve this question
    
@adamkwadsworth there is no nodejs on plnkr i guess :( – Harriz Aug 20 at 13:33
    
where's your server side route and controller? – Medet Tleukabiluly Aug 20 at 14:27
    
@evc Do you mean server side script for nodejs? i have it but i think you only need the part of write file. Controller for angularjs? i already included in my question form – Harriz Aug 20 at 14:42
1  
You need first to understand the difference between server side and client side code. Then you will realize that var nama = document.getElementById("namaFile").value makes no sense in your NodeJS code. – dfsq Aug 20 at 14:56
    
@dfsq but i did save the array to json format (please check the update question) – Harriz Aug 20 at 15:01

1 Answer 1

Make $http request to your nodejs server like that

 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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.