dpzp5127 2016-10-19 21:19
浏览 35
已采纳

Ng-submit用Routeprovider调用了两次

I use the same form to create and update users in database. I can update properly, but when I create one user, submit send twice and create two same users in database.

I have this in RouteProvider.

 .config(function($routeProvider) {
        $routeProvider
            .when("/users", {
                controller: "MainControlador",
                templateUrl: "templates/users.html"
            })
            .when("/user/:id", {
                controller: "UserControlador",
                templateUrl: "templates/user.html"
            })
            .when("/users/edit/:id", {
                controller: "UserControlador",
                templateUrl: "templates/form.html"
            })
            .when("/users/new", {
                controller: "NewUserControlador",
                templateUrl: "templates/form.html"
            })
            .when("/", {
                templateUrl: "templates/main.html"
            })
            .otherwise("/");
    })

This is my form

<div>
      <form ng-submit="saveUser()">
        <input ng-model="user.name" type="text" required>
        <input ng-model="user.surname" type="text" required>
        <input ng-model="user.phone" type="text"required>
        <input ng-model="user.email"type="text" required>
        <input type="submit">
      </form>
 </div>

And finally i use this controller

//Controller Update
.controller("UserControlador", function($scope, $routeParams, UserAPI) {
        $scope.title = "Edit user";

        UserAPI.getOne($routeParams.id).success(function(data) {
            //Get Array First Element
            $scope.user = data[0];
        })

        $scope.saveUser = function() {
            var data = ({
                id: $routeParams.id,
                name: $scope.user.name,
                surname: $scope.user.surname,
                phone: $scope.user.phone,
                email: $scope.user.email
            });
            UserAPI.update(data).success(function(data) {
                $scope.user = data;
            })
        }

    })
    //Controller Create
    .controller("NewUserControlador", function($scope, UserAPI) {
        $scope.title = "Add user";
        $scope.saveUser = function() {
            var data = ({
                name: $scope.user.name,
                surname: $scope.user.surname,
                phone: $scope.user.phone,
                email: $scope.user.email,
            });
            UserAPI.create(data).success(function(data) {
                console.log(data);
                $scope.user = data;
            })
        }
    })

¿Any idea what happen here? I tried to use console.log but apparently all works fine. I tried to use too Angular batarang to debug angular calls but don´t work.

  • 写回答

2条回答 默认 最新

  • doulou0882 2016-10-20 00:41
    关注

    A very stupid error. In my PHP code I have this...

    <?php
    $conexion = new PDO("mysql:host=localhost;dbname=httpcrud;charset=utf8", "root", "");
    $userName = $_GET['userName'];
    $userSurname = $_GET['userSurname'];
    $userPhone = $_GET['userPhone'];
    $userEmail = $_GET['userEmail'];
    $sql=$conexion->query("INSERT INTO users (name,surname,phone,email) VALUES('$userName','$userSurname','$userPhone','$userEmail')");
    $json = json_encode($sql->execute());
    echo $json;
    ?>
    

    The submit repeat insert when I use $sql->execute() and query() . I delete $json= json_encode($sql->execute()); to $json = json_encode($sql) and finally all works perfectly.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
  • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
  • ¥15 Google speech command 数据集获取
  • ¥15 vue3+element-plus页面崩溃
  • ¥15 像这种代码要怎么跑起来?
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧