3

I have locally stored a json file, im working with it but i cannot save it, loosing persistance. This code is in my web controller (angularjs)

$http.get("users.json")
            .then(function(res) {
                $scope.users = res.data;
            });

        $scope.addUser = function() {
            $scope.users.push({
                name: $scope.user
            });

            $scope.user = "";            
        }

        $scope.removeUser = function() {
            $scope.users.pop({
                name: $scope.user
            });

            $scope.user = "";            
        }

i know that another way is local storage, but i couldn't use it because i cant list every object in my html code (it says i cannot push or pop in a undefiened object).

So, how can i do it?

Thanxs!

2
  • can't save to file from browser. localStorage is stored as string so not clear what you were trying to do to push to it. You can stringify data to it however quite easily and parse back to array/object while working Commented Oct 6, 2015 at 5:25
  • 1
    JSON.stringify(dataObject) is your friend. then JSON.parse(dataAsString) to get it back. Commented Oct 6, 2015 at 14:17

3 Answers 3

2

I think, you need to initialize your users variable before calling the http request.

$scope.users = [];
$http.get("users.json")
        .then(function(res) {
            $scope.users = res.data;
        });

Hope it will fix the undefined object issue.

Sign up to request clarification or add additional context in comments.

2 Comments

not required...is being reassigned in callback which breaks original reference anyway
Right @charlietfl, I was trying to explain here 'undefiened object' error which he was getting, probably he tried to addUser and removeUser action prior to http request complete.
0

I know that another way is local storage, but i couldn't use it because i cant list every object in my html code (it says i cannot push or pop in a undefiened object).

You can use it if you store data properly by checking whether data is available or not and then adding it. Other option is if user data is going to use for different page's or controller then you can use $rootScope.

In below code you haven't define $scope.users which throws error.

// $scope.users = [];  // Uncomment code if variable is not defined
// $scope.user = '';  //  Uncomment code if variable is not defined
$http.get("users.json")
     .then(function(res) {
        $scope.users = res.data;  // Throws error if not defined earlier
});

$scope.addUser = function() {
  $scope.users.push({
    name: $scope.user   // Here $scope.user throws error if not defined earlier
  });

  $scope.user = "";
};

1 Comment

and if you use local storage with angular make sure you can mock it away in your tests by using a serivice like github.com/grevory/angular-local-storage
0

thanks yo everybody, finally i save the data in my localstorage using de json file with parse and stringify !

$scope.addUser = function() {
    var jsonData = [];

    if (localStorage.getItem("users")) {
        jsonData = JSON.parse(localStorage.getItem("users"));
    }

    jsonData.push ({
        name: $scope.user
    });

    localStorage.setItem("users",JSON.stringify(jsonData));
    $scope.users = jsonData;
    $scope.user = "";            
}

$scope.removeUser = function(user) {

    if (localStorage.getItem("users")) {
        jsonData = JSON.parse(localStorage.getItem("users"));
    }

    for (var index in jsonData) {
        if (jsonData[index].name === user.name) {
            jsonData.splice(index,1);
        }
    }

    localStorage.setItem("users",JSON.stringify(jsonData));
    $scope.users = jsonData;
    $scope.user = "";            
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.