Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i save a json file with javascript or angularjs?

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!

like image 234
vicky_864 Avatar asked Dec 03 '25 18:12

vicky_864


1 Answers

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.

like image 65
sushilprj Avatar answered Dec 06 '25 10:12

sushilprj