如何从输入框中获取值并通过单击按钮角度将其保存到数据库

时间:2019-03-14 07:04:35

标签: javascript html angularjs

我仍在学习在AngularJS上编程。 我想从输入框中获取值,然后单击我要将其保存在数据库中的按钮。 我的数据库表称为“用户” 。我有 id 用户名密码名称之类的列。 我有两个复选框。当我单击第一个复选框(编辑)时,它会显示“ div myVar” ,当我单击第二个复选框(添加新用户),其中显示“ div myVar1” 。 我想激活此按钮“添加” ,以将输入框值添加到数据库 MySQL 。请帮助我。

我有这个html代码:

<label><input type="checkbox" ng-model="myVar">Edit</label>
<label><input type="checkbox" ng-model="myVar1">Add new user</label>
    <div ng-show="myVar">
    <div class="form">
    <form ng-submit="saveItem(userForm.$valid)" name="userForm">
        <div class="row">
            <div class="col-sm-6">
                <div class="form-group">
                    <label for="database_address">User</label>
                    <input type="text" class="form-control" required ng-model="activeItem.username" placeholder="Потребителско Име..." />
                </div>

                <div class="form-group">
                    <label for="password">Password</label>
                    <input type="text" class="form-control" required id="password" ng-model="activeItem.password"  />
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label for="username">Operator</label>
                    <input type="text" class="form-control" required id="username" ng-model="activeItem.name" />
                </div>
            </div>

            </div>
            <button class="btn btn-primary" ng-disabled="userForm.$invalid" type="submit">Save</button>
            <!--<button class="btn btn-primary" ng-disabled="userForm.$invalid" type="submit">Добавяне на нов</button>-->
        </form><form ng-submit="createUser(userForm.$valid)" name="userForm1">
    <div class="row">
        <div class="col-sm-6">
            <div class="form-group">
                <label for="database_address">User</label>
                <input type="text" class="form-control1" ng-model="usernamee" placeholder="Потребителско Име..." />
            </div>

            <div class="form-group">
                <label for="password">Password</label>
                <input type="text" class="form-control1" ng-model="passwordd"required id="password"   />
            </div>
        </div>
        <div class="col-sm-6">
            <div class="form-group">
                <label for="username">Operator</label>
                <input type="text" class="form-control1" ng-model="namee" required id="username"  />
            </div>
        </div>
    </div>
    <button class="btn btn-primary" ng-click="createUser();" type="submit">Add user</button>
    <!--<button class="btn btn-primary" ng-disabled="userForm.$invalid" type="submit">Добавяне на нов</button>-->
</form>

和Angular代码:

 $scope.createUser=function()
{
    //console.log($scope.activeItem);
        //delete $scope.activeItem.hash_method
        var objectToSave = {
            username: $scope.usernamee,
            password: $scope.passwordd,
            name: $scope.namee,
            id: $scope.id
        };
         {
            defaultAdapter.query('INSERT INTO users(username,password,name,id) VALUES(username = :usernamee, password = :passwordd,name = :namee WHERE id =:id)',
              { replacements: objectToSave, type: Sequelize.QueryTypes.UPDATE }
            ).then(projects => {
              console.log(projects);
              $scope.editMode = false;
                $scope.activeItem = false;
                $scope.refresh();
            });

        }


    }

0 个答案:

没有答案