【发布时间】:2016-04-22 17:41:03
【问题描述】:
我已经从 json 文件中提取数据。现在它显示在 DOM 上。 在 HTML 页面上,我有三个选项 1) 编辑数据 2) 删除特定数据 & 3) 添加新数据。
如何使用 AngularJS 代码执行此操作?即在编辑名称时,它应该更新我的 JSON 对象。在删除行上,它应该删除 JSON 数据中的该行。如果我点击 Add New,那么输入的数据将被添加到 JSON。
我的代码如下。 通过json文件导入数据并在DOM上展示
.controller('MainCtrl', function ($scope, $http) {
$http.get('data/home.json').
success(function(data, status, headers, config) {
$scope.details = data;
}).
error(function(data, status, headers, config) {
// log error
});
});
{
"status":"success",
"adformat":[
{
"adformat_id":1,
"name":"Format 1",
"size":"300x250"
},
{
"adformat_id":2,
"name":"Format 2",
"size":"320x250"
},
{
"adformat_id":3,
"name":"Format 3",
"size":"320x480"
}
]
}
【问题讨论】:
-
你有没有尝试过?您面临的具体问题是什么?
-
试试这个模块ng-table.com/#/editing/demo-inline 它超级容易使用。但是,如果您想使用自己用 Angular 编写的方法,请告诉我。我会发布更多代码
-
如果您需要将javascript对象
$scope.details保存到JSON文件中,只需使用$http.put -
删除相当简单...见stackoverflow.com/questions/15453979/…。将其与对服务器的调用结合起来
-
@infaustus,感谢您提供 ng-table 演示。但是,它处理的是表行,但是如何更新和对 JSON 对象执行操作。一旦我有了最终的 JSON,我将调用 $http.put 保存在我的文件中。但在此之前我需要最终的 JSON 文件。