【问题标题】:JSON new data push. How to save changes? With AngularJsJSON 新数据推送。如何保存更改?使用 AngularJs
【发布时间】:2014-07-01 20:54:29
【问题描述】:

全部。我是 Angular 的新手。我需要了解如何在 push 之后保存我的 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
    }
]

【问题讨论】:

  • 客户端不能修改/删除服务器上的文件,数据应该被发回,然后由具有某种持久性的后端处理
  • 您也可以将其持久化到浏览器的localStorage或sessionStorage中。
  • 这就是我现在得到的 - localStorage 节省。但是如何在服务器中进行更改?

标签: javascript json angularjs factory


【解决方案1】:

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

第一:您可以创建一个按钮,说“保存更改”,并在完成添加新数据的过程后告诉用户您的所谓功能addReview,点击save changes,点击这个按钮,你可以创建一个 $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

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

【讨论】:

  • 快到了。我收到警报,但在 JSON 中看不到任何变化。任何想法为什么?
  • app.controller('ReviewController',['$http', function($http){ this.r = {}; this.addReview = function(product){ this.r.createdOn = Date.now(); product.reviews.push(this.r); var store = this; $http.post('/products.json',store.r) .success(function(){ alert('Your Changes have been saved to database'); }); this.r = {}; }; }]);我这样做对吗?
  • 我认为你是在将一个空数组推入你的底层
  • 您已将 this.r 定义为一个空对象,然后在您的 addReview 函数中,您正在推动这个空对象为什么??????
  • 根据您的代码,我认为您必须将您的产品推送到您的 product.reviews 中,您正在推送的 this.r 是空的:(
猜你喜欢
  • 1970-01-01
  • 2023-03-02
  • 2017-09-01
  • 2018-01-14
  • 1970-01-01
  • 2023-03-26
  • 1970-01-01
  • 2016-08-01
  • 1970-01-01
相关资源
最近更新 更多