[英]Want to submit data from a form as a new Object in a JSON array using Angular
I am looking to create a simple input box with a submit button that will add information to a JSON file. 我正在寻找一个带有提交按钮的简单输入框,以将信息添加到JSON文件中。 I am using Angular to retrieve that data, create a new JSON object and post this object with the new data to a file called pets.json.
我正在使用Angular检索该数据,创建一个新的JSON对象,并将带有新数据的该对象发布到名为pets.json的文件中。
HTML 的HTML
<form ng-submit="submit()" ng-controller="PetPosting">
Enter text and hit enter:
<input type="text" ng-model="text" name="text" />
<input type="submit" id="submit" value="Submit" />
</form>
Angular JS 角JS
var app = angular.module('app', ['ui.bootstrap', 'bootstrapLightbox']);
//load images from pets.json and display them in HTML
app.controller('GalleryCtrl', function ($scope, Lightbox, $http) {
$http.get('pets.json')
.then(function(res){
$scope.images = res.data;
});
});
/*Add new data to JSON file*/
app.controller('PetPosting',function($scope, $http) {
$scope.submit = function() {
if ($scope.text) {
$http({
method: "post",
url: "pets.json",
data: {
"thumbUrl": $scope.text,
}
});
}
}
});
I don't understand fully how this works. 我不完全了解这是如何工作的。 Any insight or direction on how to figure out how to do this simple task would be greatly appreciated :)
对于如何弄清楚如何完成此简单任务的任何见解或指导,将不胜感激:)
Thats not the correct implementation of "$http", the "url" property is a server endpoint that receives your Data, for that you would need some server-side code to append to the .json file. 那不是“ $ http”的正确实现,“ url”属性是一个接收数据的服务器端点,因为您需要一些服务器端代码才能附加到.json文件。
If your wanting to save information Local to the user the LocalStorage API maybe sufficient and is very easy to implement 如果您想将信息保存到用户本地,则LocalStorage API可能就足够了,并且很容易实现
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.