简体   繁体   English

使用AngularJS将数据直接发布到JSON中

[英]Post Data with AngularJS into JSON directly

I'm still new in AngularJS, so far I've succeeded to fetch data from JSON in my Controller 我仍然是AngularJS的新手,到目前为止,我已经成功从Controller中的JSON获取数据

var testAppApp = angular.module('testAppApp', []);
testAppApp.controller('SimpleController', function ($scope, $http) {
        $http.get('data/data.json')
        .then(function(takeprocess) {
            $scope.customers = takeprocess.data.records;
        });
});

here my JSON format 这是我的JSON格式

{ 
"records": [
{
  "Name": "Alfreds Futterkiste",
  "City": "Berlin",
  "Country": "Germany"
},
{
  "Name": "Ana Trujillo Emparedados y helados",
  "City": "México D.F.",
  "Country": "Mexico"
},
............
}

My index.html 我的index.html

<div ng-controller="SimpleController">
Search <input type="text" ng-model="filter.name" /> 
<ul>
<li ng-repeat="cust in customers | filter:filter.name | orderBy:'No'">
{{$index +1}} - {{cust.Name}} - {{cust.City}} - {{cust.Country}}</li>
</ul> 
Adding New Customer <br/>
Name: <br/>
<input type="text" ng-model="Name" /><br/>
City: <br/>
<input type="text" ng-model="City" /><br/>
Country: <br/>
<input type="text" ng-model="Country" /><br/> 
<button ng-click="addCustomer(Name, City, Country)">Add Customer</button>
</div>

Here my output 这是我的输出

I want create function addCustomer to post data, and (maybe) store the data into JSON, but I don't know how create the function... in many ways, some people talk angularJS is Front End framework, it can't store data to JSON directly. 我想创建函数addCustomer来发布数据,并且(也许)将数据存储到JSON中,但是我不知道如何创建该函数...在许多方面,有人说angularJS是前端框架,它无法存储数据直接发送到JSON。 Please suggest me to make a point, In my case I want put new data into list and permanent 请建议我提出一点,就我而言,我想将新数据放入列表并永久保存

When you say "store the data into JSON" do you mean put it into the data.json file directly? 当您说“将数据存储到JSON”时,是指直接将其放入data.json文件吗?

If yes, Javascript is a front end language and doesn't have access to the file system that hosts it. 如果是,则Javascript是一种前端语言,并且无权访问托管它的文件系统。 The solution would be to run a back end language like Go, Node.JS or PHP to accept a post request and write that data to your data.json. 解决方案是运行诸如Go,Node.JS或PHP之类的后端语言来接受发布请求,并将该数据写入data.json。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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