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条)

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b