简体   繁体   English

表单中的数据不会传递给提交函数 - 使用 Angular

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

I have a problem with a form implemented using Angular.我对使用 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.在范围内定义我的变量我可以在 html 表单(ng-model)中看到值(预填充),但是当我提交函数时,新数据(由用户插入)不会更新模型($scope var) 在控制器中。 Here a snipped of my html and js controller:这是我的 html 和 js 控制器的片段:

<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) { .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.基本上我没有双向同步 var,只是从控制器到视图而不是从视图到控制器。

Any advice?有什么建议吗? Cheers, Giovanni干杯,乔瓦尼

I suppose $scope.startProcess is your submit function.我想$scope.startProcess是您的提交功能。 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.您已在函数外部编写此代码,因此它正在获取$scope.ticketDetails变量的初始数据。

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM