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

时间:2014-07-01 20:54:30

标签: javascript json angularjs factory

所有。我是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数据。我不知道你知道这对你有用吗<​​/ p>

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
    }
]

1 个答案:

答案 0 :(得分:1)

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

首先:您可以创建按钮,称为&#34;保存更改&#34;,并在完成向您添加新数据的过程后告诉用户所谓的功能 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

最好为这种理论定义一个工厂/服务(从某个地方推送/添加/发送/删除somethig)