[英]Angularjs form in controller always undefined
In my project I use ui-view and with following structure (deleted unnecessary code): 在我的项目中,我使用ui-view并具有以下结构(删除了不必要的代码):
<main class="content">
<div class="inner" ng-controller="OrderPageController">
<div ui-view="main-info"></div>
<div ui-view="comments"></div>
<div ui-view="bids-list"></div>
<div ui-view="add-bid"></div>
</div>
</main>
And in add-bid view: 在加价竞标视图中:
<form name="newbidform" novalidate ng-submit="postNewBid(newbidform);">
<input type="text" ng-model="newbid.bid" required>
<input type="submit">
</form>
So, then I submit form I try to check if all of required inputs are valid: 因此,然后我提交表单,尝试检查所有必需的输入是否有效:
$scope.postNewBid = function(form) {
console.log(form) // $valid and $invalid always undefined
console.log($scope.newbidform) // always undefined
// check validity before send
if (!form.$valid) {
angular.forEach(form.$error, function (field) {
angular.forEach(field, function(errorField){
errorField.$setTouched();
})
});
$scope.failedSubmit = true;
return false;
} else {
$scope.failedSubmit = false;
}
// other things if form is valid
So, the problem the form is always undefined (at all or $valid/$invalid attr). 因此,表单的问题始终是不确定的(根本就是$ valid / $ invalid attr)。 I tried to use formName as parameter in function, as $scope.formName variable (always undefined), and define controller twice, in second time on form:
我试图将formName用作函数中的参数,用作$ scope.formName变量(始终未定义),并在窗体上第二次定义了两次控制器:
<form name="newbidform" novalidate ng-submit="postNewBid();" ng-controller="OrderPageController">
Actually, it works, but when I'm trying to access other variables in controller - I can't. 实际上,它可以工作,但是当我尝试访问控制器中的其他变量时,我做不到。 So, is there a way to get a form state in controller in AngularJs?
那么,有没有办法在AngularJs的控制器中获取表单状态? Thanks
谢谢
newbid is the variable you want to get, right ? newbid是您想要获取的变量,对吗?
Try : 尝试:
<form name="newbidform" novalidate ng-submit="postNewBid(newbid);">
It sounds like your form is nested in child scope such as inside an ng-include
or ng-if
in which case since the form object doesn't exist in the parent scope the parent won't see it when angular creates it in the child. 听起来您的表单嵌套在子范围内,例如在
ng-include
或ng-if
在这种情况下,因为在父范围内不存在表单对象,所以当angular在子范围内创建父对象时,父对象将看不到。
You can fix it by simply creating an empty object in the parent controller and that object will be inherited by child scopes. 您可以通过在父控制器中简单创建一个空对象来解决该问题,该对象将被子作用域继承。 Since the object exists now, a new object won;t need to be created and all the properties will get added to the reference in child scope.
由于该对象现在存在,因此将不需要创建新对象,并且所有属性都将添加到子作用域中的引用。
$scope.newbidform ={};
$scope.postNewBid = function(form) {...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.