gpt4 book ai didi

javascript - JSON新数据推送。如何保存更改?使用 AngularJs

转载 作者:行者123 更新时间:2023-12-03 12:19:23 26 4
gpt4 key购买 nike

全部。我是 Angular 的新手。我需要了解如何在推送后保存我的 JSON 更改。

app.controller('ReviewController', function(){
this.r = {};
this.addReview = function(product){
this.r.createdOn = Date.now();
product.reviews.push(this.r);
this.r = {};
};
});

我也有这个东西,我可以在其中获取 JSON 数据。我不知道知道这一点对您是否有用

app.factory('events', function($http) {
return function(){
return $http.get('products.json');
};
});
app.controller("StoreController", function(events){
var store = this;
store.products = [];
events().success(function(data) {
store.products = data;
});

还有一件事。我的 products.json 文件:

[
{
"title": "Rubinas",
"price": 199.99,
"description": "Labai kietas, davai pirkit.",
"images": {
"full": "images/gem2.gif",
"thumb": "images/gem2t.gif"
},
"reviews": [
{
"stars": 1,
"body": "Bullshit",
"author": "Hater",
"createdOn": 1397490980837
},
{
"stars": 3,
"body": "Simple stone!",
"author": "Dude",
"createdOn": 1397490980837
},
{
"stars": 5,
"body": "Amazing.",
"author": "Kate",
"createdOn": 1397490980837
}
],
"canPurchase": true,
"soldOut": false
},
{
"title": "Smaragdas",
"price": 499.99,
"description": "Oho, koks.",
"images": {
"full": "images/gem3.gif",
"thumb": "images/gem3t.gif"
},
"reviews": [
{
"stars": 5,
"body": "This is so amazing",
"author": "Steeler",
"createdOn": 1397490980837
},
{
"stars": 4,
"body": "Pretty cool!",
"author": "Jim",
"createdOn": 1397490980837
},
{
"stars": 2,
"body": "I don't like it very much.",
"author": "Kim",
"createdOn": 1397490980837
}
],
"canPurchase": true,
"soldOut": false
},
{
"title": "Deimantas",
"price": 9999.99,
"description": "Labai kietas, na bet rimtai.",
"images": {
"full": "images/gem4.gif",
"thumb": "images/gem4t.gif"
},
"reviews": [
{
"stars": 1,
"body": "Eww...",
"author": "Hater",
"createdOn": 1397490980837
},
{
"stars": 4,
"body": "Nice!",
"author": "Dude",
"createdOn": 1397490980837
},
{
"stars": 5,
"body": "So unbelievible.",
"author": "Matt",
"createdOn": 1397490980837
},
{
"stars": 2,
"body": "I don't like it.",
"author": "Steven",
"createdOn": 1397490980837
}
],
"canPurchase": true,
"soldOut": false
}
]

最佳答案

有一些方法可以做到这一点

首先:您可以创建一个名为“保存更改”的按钮,并在使用所谓的函数完成添加新数据的过程后告诉用户< strong>addReview,点击保存更改,然后单击此按钮,您可以创建 $http.post 请求并将新推送的数组发送到数据库中(或你的 json 文件,无论它是什么)

第二:您可以在自己的函数 addReview 中执行相同的 $http.post 操作,而无需创建按钮并监听用户单击保存更改按钮。

例如: 如果您想采用我的第二个解决方案:

       this.addReview = function(product){
this.r.createdOn = Date.now();
product.reviews.push(product);
this.r = {};
$http.post('yourServerFile',WhateverYouWantToSave)
.success(function(){
alert('Your Changes have been saved to database');
})
};
//Dont forget to inject $http into your controller

最好为这种理论定义一个工厂/服务(从某处推送/添加/发送/删除某些东西)

关于javascript - JSON新数据推送。如何保存更改?使用 AngularJs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24519117/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com