我有一个简单的表格:
<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
。
要做一些简单的事情,但我不知道我哪里有错误。
答案 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);
}