使用angularJS进行CRUD操作

时间:2015-03-28 09:57:56

标签: javascript asp.net angularjs

我正在使用Asp.net中 angularJS c#创建简单CRUD操作的应用程序。
我想知道,在哪里我应该保留CRUD操作的代码?在.js文件中?
我还需要一个任何一个CRUD操作的小演示 请注意,我使用的是 SQL数据库

有人能帮助我吗?

3 个答案:

答案 0 :(得分:2)

答案 1 :(得分:1)

您不需要将代码放在.js文件中。

但是如果你想这样做,你需要在你执行CRUD的aspx页面中引用该文件。

以下是CRUD using AngularJS

的最佳示例

答案 2 :(得分:0)

<script type="text/javascript">
    var myapp = angular.module('myapp', []);
    myapp.controller('mycontroller', function ($scope) {
        $scope.employees = [];
        $scope.AddDetails = function (employee)
        {

                $scope.employees.push(employee);
                $scope.employee = '';

        }
        $scope.DeleteDetails = function (employee)
        {

            $scope.employees.splice($scope.employees.indexOf(employee), 1)
        }
        $scope.EditDetails=function(employee)
        {
            $scope.employee = employee;

        }

        $scope.UpdateData = function () {
            $scope.employee = '';
        }

            //ClearModel();

        function ClearModel() {
            $scope.employees.Name = '';
            $scope.employees.Email = '';
            $scope.employees.Location = 0;
        }



    });
</script>

,这是我的HTML视图。

<div ng-app="myapp" ng-controller="mycontroller">
    <div>
        Name
    </div>
    <div>
        <input id="txt_name" ng-model="employee.Name" name="name" />
    </div>
    <div>
        Email
    </div>
    <div>
        <input id="txt_email" ng-model="employee.Email" name="email" />
    </div>
    <div>
        Location
    </div>
    <div>
        <input id="txt_loc" ng-model="employee.Location" name="location" />
    </div>
    <div>

    <button type="button" id="Btn_save" class="btn btn-primary" ng-click="AddDetails(employee)">Save</button>
        <button type="button" id="Btn_Update" class="btn btn-primary" ng-click="UpdateData(employee)">Update</button>
    <div>
        <table class="table-bordered" width="600px">
            <thead>
                <tr>
                    <td>USER NAME</td>
                    <td>EMAIL ID</td>
                    <td>LOCATION</td>
                    <td>EDIT/DELETE</td>
                </tr>
            </thead>

            <tr ng-repeat="employee in employees">
                <td>
                    {{employee.Name}}
                </td>
                <td>
                    {{employee.Email}}
                </td>
                <td>
                    {{employee.Location}}
                </td>
                <td>
                    <button type="button" class="btn btn-primary" ng-click="EditDetails(employee);hideMe()">Edit</button>
                    <button type="button" class="btn btn-success" ng-click="DeleteDetails(employee)">Delete</button>
                </td>

            </tr>
        </table>
    </div>

</div>