1

I am trying to insert data to database through angularJS but the data inserted twice.. I have tried to use ngRoute but still I face the same problem.

app.js

 var app = angular.module("addDepApp", []);
 app.controller('insertDepCtl', function($scope, $http) {

var isSend = false;
$scope.$on('newuser', function(event, data){
    load(true);

});


var load = function(isEvent){

    if($scope.$parent.newuser != null){
        isSend = true;
    }
};
load();

$scope.insertDepartment = function () {
    console.log("called insertDepartment");
    if (isSend == true){
        $scope.newuserSend = {'org_id': $scope.$parent.newuser.org_id, 'dep_name': $scope.department};
        $http.post("http://192.168.1.12:8888/XXXX/XXX/insertDep.php/",$scope.newuserSend)

    }

}

});

add.html

  <body ng-app="addDepApp">
  <div class="12u$" ng-controller="insertDepCtl">
  <input type="button" value="تسجيل" class="special" id="signup" ng-click="insertDepartment()"/>
  </div>
  </body>
2
  • provide a demo that reproduces problem in client Commented Oct 1, 2016 at 20:40
  • @charlietfl 2 rows inserted in the database one row with empty data and the second row with data I have send Commented Oct 2, 2016 at 4:36

2 Answers 2

2

Remove ng-controller="insertDepCtl" from your html code, your router injects this for you. Right now, you're calling everything twice

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, but I have removed the ngRoute
is there any solution?
I got this error "insertDepartment is undefined" .. Please note that I am not using ngRoute
any solution you suggest?
0

I have changed the code to this and now it works fine!!

 $scope.insertDepartment = function () {
    if (isSend == true) {

        var request = $http({
            method: "post",
            url: "http://192.168.1.106:8888/XXXX/XXX/insertDep.php/",
            data: {'org_id': $scope.$parent.newuser.org_id, 'dep_name': $scope.department},
            headers: {'Content-Type': 'application/x-www-form-urlencoded'}
        });
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.