简体   繁体   中英

AngularJS update JSON file

I have a JSON file in my server and an angularJS code that reads it like this:

 $http.get('route/to/json/file.json').success(function(data) {

            $scope.watchesdata = data;

            var tmpList = [];

            for (var i = 0; i <= limit; i++){
            tmpList.push({
                Time: $scope.watchesdata[i].balance,
                Company : $scope.watchesdata[i].company,
                Matter: $scope.watchesdata[i].address
            });
          }  

            $scope.list = tmpList;    

        });   

After some more code that is not important, I update an specific 'Time' value and i want the JSON file on my server to update its information too, something like :

"If company==something and matter==something then oldTime = newTime"

I've just found how to add data at the end of a JSON, but i need to look for a specific entry and modify just one field.

Assuming I'm understanding your question correctly, something like this would work:

function updateTime(company, matter, newTime) {
    $scope.list.forEach(function (item) {
        if (item.Company == company && item.Matter == matter) {
            item.Time = newTime;
        };
    });
};

You would have to POST this data back to your server, as JSON, to update your server side though. It's unclear if you are asking how to do that part too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM