表单提交后,AngularJS数据未定义

时间:2017-09-01 19:18:46

标签: angularjs

我有一个简单的表格:

<form ng-cloak class="form-horizontal" name="regForm" ng-submit="submit()" novalidate>
<input type="text" class="form-control" id="pociFirstName" placeholder="First Name" name="pociFirstName" ng-model="poc.firstName" ng-required="true" ng-class="{red: regForm.pociFirstName.$invalid && (regForm.$submitted || regForm.pociFirstName.$touched)}" />
<input type="text" class="form-control" id="pociLastName" placeholder="Last Name" name="pociLastName" ng-model="poc.lastName" ng-required="true" ng-class="{red: regForm.pociLastName.$invalid && (regForm.$submitted || regForm.pociLastName.$touched)}" />
<input type="text" class="form-control" cc-number cc-eager-type cc-type="cardType" id="ccNum" placeholder="Card Number" name="ccNum" ng-model="card.number" ng-required="true" ng-class="{red: regForm.ccNum.$invalid && (regForm.$submitted || regForm.ccNum.$touched)}" ng-init="card.number=''"/>

....

<button class="btn btn-primary btn-payment" type="submit">Submit</button>   

</form>

然后我开始参与控制器

$scope.regForm = {};
$scope.poc = {};
$scope.card = {};

$scope.submit = function () {

    console.log("Form submited");

    $scope.regForm = regForm;
    $scope.poc = regForm.poc;
    $scope.card = regForm.card;

    console.log("data: " + $scope.poc);
}

但是在表单提交后我无法从表单中获取数据 - 它来自undefinied

要做一些简单的事情,但我不知道我哪里有错误。

1 个答案:

答案 0 :(得分:2)

应该是这样的。您可以ng-model访问$scope值。因此,访问ng-model="poc.firstName"ng-model="poc.lastName"只需使用$scope.poc,访问ng-model="card.number"即可使用$scope.card

$scope.submit = function () {
  console.log("Form submited");
  console.log("data: " + $scope.poc);
}