gxvigo
gxvigo

Reputation: 837

Data from form are not passed to submit function - Using Angular

I have a problem with a form implemented using Angular. defining my variable in the scope I can see value (pre-filled) in the html form (ng-model), but when I submit the function, the new data (inserted by the users) don't update the model ($scope var) in the controller. Here a snipped of my html and js controller:

<form class="form-horizontal">
 <h3>Citizen</h3>
 <div class="row">
   <div class="form-group">
     <label for="name" class="col-sm-2 control-label">Name</label>
     <div class="col-sm-4">
     <input type="text" class="form-control" id="name" ng-model="ticketDetails.ticketDetails.name" placeholder="Name">
   </div>
   <label for="DOB" class="col-sm-2 control-label">Birth date</label>
   <div class="col-sm-4">
     <input type="date" class="form-control" id="DOB" ng-model="ticketDetails.ticketDetails.DOB" placeholder="Birth Date">
   </div>
  </div>
</div>...

and the controller

angular.module('demoApp', [])

.controller('mainController', function($scope, $http) {

//    $scope.ticketDetails = { "ticketDetails" : {
//        "name": "Giovanni Vigorelli",
//        "DOB": "1974-05-02T05:07:13Z",
//        "driverLicense": "e345234",
//        "registration": "hdd843",
//        "ticketType": "Speeding",
//        "date": "2016-05-02T05:07:13Z",
//        "location": "34 Queen St, Auckland",
//        "ticketId": "12345",
//        "officer": "Oscar Nice"
//    }};


$scope.ticketDetails = { "ticketDetails": {}};

$scope.ticketDetails.ticketDetails.ticketId = (+new Date).toString(36).slice(-5);
// The following should be the authentucated user
$scope.ticketDetails.ticketDetails.officer = "Oscar Nice";


var bpmQueryParam = 'action=start&bpdId=25.c1206b63-1e94-4aaa-9dc1-76363270b441&processAppId=2066.d0e91cc6-a515-4965-ba6f-516bdbddcb00&params=' + JSON.stringify($scope.ticketDetails) + '&parts=all';


$scope.startProcess = function(){

    console.log('### In startProcess');
    console.log("### bpmQueryParam: " + bpmQueryParam);

    var req = {

        method: 'POST',
        headers:  {'Authorization': 'Basic YWRtaW46YWRtaW4=','Accept': 'application/json','Access-Control-Allow-Origin': '*', 'Content-Type': 'application/x-www-form-urlencoded'},
        url: 'http://1.1.1.1:9080/rest/bpm/wle/v1/process',

        data: bpmQueryParam
    }

Basically I don't a bidirectional sync of the var, just from controller to view and NOT from view to controller.

Any advice? Cheers, Giovanni

Upvotes: 1

Views: 79

Answers (1)

Babu Swami
Babu Swami

Reputation: 798

I suppose $scope.startProcess is your submit function. Please write the following line inside the submit function:

var bpmQueryParam = 'action=start&bpdId=25.c1206b63-1e94-4aaa-9dc1-76363270b441&processAppId=2066.d0e91cc6-a515-4965-ba6f-516bdbddcb00&params=' + JSON.stringify($scope.ticketDetails) + '&parts=all';

You have wriiten this code outside the function, therefore it is taking the initial data for $scope.ticketDetails variable.

You should write it as following:

$scope.startProcess = function(){
    var bpmQueryParam = 'action=start&bpdId=25.c1206b63-1e94-4aaa-9dc1-76363270b441&processAppId=2066.d0e91cc6-a515-4965-ba6f-516bdbddcb00&params=' + JSON.stringify($scope.ticketDetails) + '&parts=all';
    // Rest of your code
}

Upvotes: 1

Related Questions