我无法获取 ngDialog 中的表单名称。我的对话框是:
$scope.dialogAddNewParent = ngDialog.open({
template: 'addNewParentDialog',
className: 'ngdialog-theme-default dialogwidth550',
scope: $scope
});
问题是在 ngDialog 中我有一个带有 name="addNewParentForm" 的 from,但是当我试图向我的控制器验证这个表单时,我无法获得我的表单的名称,所以我收到了这个错误。
Cannot read property 'lastName' of undefined
lastName 是我的表单输入的名称。
在我的 html 中,我有这个对话框的脚本:
<script type="text/ng-template" id="addNewParentDialog">
<div class="p-lg" style="color:#748690">ADD NEW PARENT</div>
<form name="addNewParentForm" ng-submit="addNewParent(addNewParentForm.$valid)" novalidate class="form-validate form-horizontal p-lg">
<div class="form-group">
<label style="color:#1a9fc8" class="col-sm-3 control-label">Last Name*</label>
<div class="col-sm-9">
<input type="text" name="lastName" ng-model="new_parent.lastName" required="" ng-maxlength=80 class="form-control" value="{{new_parent.lastName}}" />
<span ng-show="addNewParentForm.lastName.$dirty && addNewParentForm.lastName.$error.required" class="text-danger">This field is required</span>
<span ng-show="addNewParentForm.lastName.$dirty && addNewParentForm.lastName.$error.maxlength" class="text-danger">Input cannot be longer than 80 characters</span>
</div>
</div>
<div class="form-group">
<label style="color:#1a9fc8" class="col-sm-3 control-label">First Name*</label>
<div class="col-sm-9">
<input type="text" name="firstName" ng-model="new_parent.firstName" required="" ng-maxlength=80 class="form-control" value="{{new_parent.firstName}}" />
<span ng-show="addNewParentForm.firstName.$dirty && addNewParentForm.firstName.$error.required" class="text-danger">This field is required</span>
<span ng-show="addNewParentForm.firstName.$dirty && addNewParentForm.firstName.$error.maxlength" class="text-danger">Input cannot be longer than 80 characters</span>
</div>
</div>.......
在我的控制器中
....$scope.addParent = function (choice) {
switch (choice) {
case 1:
$scope.dialogAddNewParent = ngDialog.open({
template: 'addNewParentDialog',
className: 'ngdialog-theme-default dialogwidth550',
scope: $scope
});
break;
case 2:
$scope.dialogAddNewParentFromList = ngDialog.open({ template: 'addNewParentFromListDialog', controller: 'StudentEditController', className: 'ngdialog-theme-default dialogwidth350 ngdialog-overlay', overlay: false, scope: $scope, data: {} });
break;
}
}
$scope.addNewParent = function (valid) {
$scope.submitted = true;
var input = $scope.addNewParentForm;
if (valid) {
$scope.dialogAddNewParent.close();
$scope.parents.push($scope.new_parent);
console.log('Parents', $scope.parents)
} else {
console.log('Not valid!!');
$scope.addNewParentForm.lastName.$dirty = true;//here is the problem with undefined
}
};.....
谢谢!