我有json文件:
test.json:
{
"MyTest": [{
"Main": {
"static": {
"name": "TestName1"
},
"dynamic": {
"testkey01": "testkey01data",
"testkey02": 40,
"testkey03vals": [1, 1, 1]
}}
}, {
"Main": {
"static": {
"name": "TestName2"
},"dynamic": {
"testkey01": "test01value",
"testkey03": 10,
"testflags": ["Test"]
}}
}, {
"Main": {
"static": {
"name": "TestName3"
},"dynamic": {
"testkey01": "testkey01value for TestName3",
"testnumber": 30
}}
}]
}
我想使用 AngularJS 对此 json 数据执行添加、编辑/更新和删除操作。
我做了以下事情:
索引.html:
<!DOCTYPE html>
<html>
<head>
<script data-require="angular.js@1.5.x" src="https://code.angularjs.org/1.5.8/angular.js" data-semver="1.5.8"></script>
<script src="app.js"></script>
</head>
<body ng-app="myApp">
<div ng-controller="TestCtrl">
<table>
<thead>
<tr>
<th>Name</th>
<th>Edit</th>
<th>Add</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="(key, value) in myTestJson.MyTest" >
<td>{{value.Main.static.name}} </td>
<td><a ng-href="editName.html">Edit</a></td>
<td><button id="button1" ng-click="add(value.Main.static.name)">Add</button></td>
</tr>
</tbody>
</table>
</div>
<br><br>
<br>
<table border="1" class="table">
<thead>
<tr>
<th>Name</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="name in jsonNames" >
<td>{{name}}</td>
<td><button id="button1" name="singlebutton" ng-click="delete(name)">Delete</button></td>
</tr>
<tr ng-hide="myTestJson.MyTest.length">
<td colspan="3">
<p>No Names</p>
</td>
</tr>
</tbody>
</table>
</body>
</html>
编辑名称.html:
<!DOCTYPE html>
<html>
<title>Edit and Update JSON data</title>
<div>
<table><tbody>
<tr ng-repeat="(key, value) in myTestJson.MyTest.Main.dynamic" >
<td><label>{{key}}: </label>
<input placeholder="" type="text" ng-model="value">
</td>
</tr>
</tbody>
</table>
<button value="Update and Save" id="saveButtonId" ng-href="index.html" ng-click="finishEdit">Update/Save</button>
</div>
</html>
应用程序.js:
var app = angular.module('myApp', []);
app.controller('TestCtrl',function($scope, $http ) {
$http.get('test.json').success(function(response) {
$scope.myTestJson = response;
// console.log(JSON.stringify(response));
$scope.add = function (){
alert("add is called");
//$scope.myTestJson.push($scope.jsonNames);
$scope.myTestJson.push($scope.myTestJson, jsonNames);
};
$scope.delete = function (index){
$scope.myTestJson.splice(index,1);
alert("JSON Name is deleted");
}
$scope.saveUpdate = function (index) {
$scope.myTestJson[index] = $scope.dynamiceditedModel;
$scope.edited = -1;
};
//$scope.dynamiceditedModel=$scope.myTestJson;
});
});
一个。如果我单击Add按钮:则应在我的第二个表中添加相应的 JSON 名称数据。
湾。如果我单击Edit按钮:则相应选择的 JSON 名称“ dynamic”字段选项应该是可编辑的(打开editName.html),然后应该在单击更新/保存按钮时保存(然后它应该被重定向到index.html)。
C。如果我单击Delete按钮:则应删除相应的 JSON 名称。
我创建了Plnkr。我请求大家帮我解决这个问题,我该如何执行这些操作。提前致谢。