2015-11-17 48 views
1

我要更新AngularJS如何更新AngularJS

现有的JSON文件JSON文件

JSON文件:

{ 
    "text1": "Click here to edit!", 
    "text2": "Click here to edit!", 
    "text3": "Click here to edit!", 
    "text4": "Click here to edit!" 
} 

我要更新这个JSON文件:

text1: "Abc" 

并将此更改保存在JSON文件中

+1

是服务器的JSON文件? –

+0

@CharlieH是的.. – Gaurav

+0

@Gaurav快速问题 你想发送更新/修改的json到服务器? –

回答

1

想象一下,您有getjson.phpsavejson.php在服务器中的工作完全按照他们的名字建议。

现在使用$http服务Angular从服务器上检索你的json。再次

$http.get("getjson.php").then(function(response){ 

    $scope.myJsonObject = response.data; 

    //Your json becomes JS object here. Change it the way you want 
    $scope.myJsonObject.text1 = "Abc"; 

}); 

使用$http服务来发送你的JSON回服务器。

$http({ 
      method: "post", 
      url: "savejson.php", 
      data: $scope.myJsonObject, 
      headers: { 'Content-Type': 'application/json; charset=utf-8' } 
}); 

这是基本的。请注意,您需要执行php部分来保存/加载您的json文件。你也应该处理$http服务的错误。

请参阅$http服务和promises的工作方式。

+0

我想读取和更新相同的json文件。 – Gaurav

+1

只要'getjson.php'和'savejson.php'处理同一个文件,它就是相同的json文件。 –