json对象集合(添、删、改)

来源:互联网 发布:笛佛软件面试 编辑:程序博客网 时间:2024/06/16 09:03
<!doctype html><html ng-app><head>    <title></title>    <script type="text/javascript" src="angular.min.js"></script>    <script type="text/javascript">        function my_controller($scope) {            $scope.students = [                { id: 1, name: '张一' },                { id: 2, name: '李二' }            ];            $scope.add_student = function () {//添加                var student = { id: 3, name: '余三' };                $scope.students.push(student);            }            $scope.delete_student = function (_id) {//删除                var index = $scope.get_index(_id);                $scope.students.splice(index, 1);            }            $scope.update_student = function (_id) {//修改                var index = $scope.get_index(_id);                var student = { id: _id, name: '马二' };                $scope.students.splice(index, 1, student);            }            $scope.get_index = function (_id) {//获取对象在数组中index                if ($scope.students.length > 0) {                    for (var i = 0; i < $scope.students.length; i++) {                        if ($scope.students[i].id == _id) {                            return i;                        }                    }                }            }        }    </script></head><body ng-controller="my_controller">    <input type="button" value="add" ng-click="add_student();" />    <input type="button" value="delete" ng-click="delete_student(1);" />    <input type="button" value="update" ng-click="update_student(2);" />    <table>        <tr ng-repeat="item in students">            <td>{{item.id}}</td>            <td>{{item.name}}</td>        </tr>    </table></body></html>

0 0