【问题标题】:Making 'Add new user', JSON data overwrites the current data..needs to be added.--AngularJS制作'添加新用户',JSON数据覆盖当前数据..需要添加.--AngularJS
【发布时间】:2014-05-09 15:37:40
【问题描述】:

我是 AngularJS 的新手。在单击“添加新成员”按钮后尝试将表单数据发送到 Json 文件时,新的 Json 数据会覆盖当前现有的 Json 数据。我需要在最后一个数据之后添加数据。 我使用了下面的代码

var memberControllers = angular.module('memberControllers', []);`memberControllers.controller('addListCtrl', ['$scope', '$http',  '$location',
    function($scope, $http, $location){
        $scope.members = [];
        $scope.formData = {};
        $scope.addMember = function(){
            $http.post('data/members.json', $scope.formData).

                success(function(data){
                    console.log('added ' + data);
                    $scope.formData = {};
                    $scope.members = data;
                    $scope.members.push(data);

                })
                .error(function(data){
                    console.log('Error: ' + data);
                });
                $location.path('/members');

        /*});*/

    };

    }]);`

结果来自Json文件--->

{"id":"1","name":"Jane" }

我希望在下面--->

[{"id":"1","name":"Jane"},{"id":"2","name":"John"},{"id":"3","name":"Tom"}]

【问题讨论】:

    标签: json angularjs http post


    【解决方案1】:

    $scope.members = data; 覆盖 members - 省略这一行,然后使用 push,就像你之后所做的那样。

    【讨论】:

    • 感谢及时回复,还是一样。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-15
    • 2021-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多