mtahir08
mtahir08

Reputation: 87

I want to Pass value to JSON in angularjs

Please give me some example of $http.post. I am new in AngularJS. I want to update a JSON file while retrieving information from user. I want to update values but unable to update a JSON file. My JSON file is data.json.

    { var app=angular.module("APP",[]); 
 app.controller("APPController", function ($scope, $http){
   $scope.add = function(){
   var dataObj={ model:$scope.addModel,
    car: [$scope.option1, $scope.option2, $scope.option3, $scope.option4],
    type: $scope.Type};  
$http.post("data.json",dataObj)  
.success(function(res){console.log("success"+res.records)})  .error(function(res){console.log("error")}); 
 };
   });
  }

and here is my data.json file

    {
  "records": [
    {
      "model" : "car model",
      "car" : ["num1","num2","num3","num4"],
      "type" : "mode type"
    },
    {
      "model" : "car model",
      "car" : ["num1","num2","num3","num4"],
      "type" : "mode type"
    },
]
}

Upvotes: 0

Views: 660

Answers (2)

Aniket Sinha
Aniket Sinha

Reputation: 6031

You don't POST data on a JSON file, you make the POST API call to a server. You can GET data from a JSON file.

I suggest you read through Angular's POST documentation first.

Upvotes: 0

Ashish Kumar
Ashish Kumar

Reputation: 3039

You can pass the data object in 2nd param of the $http.post() method.

I am confused to get the exact context of update JSON. But if you want to update the existing variable array in your controller which you are using in view from the response, you can do it it JavaScript way.

Here is a working example for the same. This might help.

JSFiddle: http://jsfiddle.net/ashishanexpert/zvcx5z38/2/

Upvotes: 1

Related Questions