简体   繁体   English

在Angular中,如何在$ scope中的控制器中获取表单?

[英]In Angular, how to get a form in $scope in controller?

Having these two files: 具有以下两个文件:

HTML: HTML:

<form name="registrationForm" ng-submit="registerUser()">
   ...
</form>

JS (inside the Angular controller): JS(在Angular控制器内部):

$scope.registrationForm.confirmPassword.$setValidity("PasswordsMatch", $scope.validation.doPasswordsMatch);

I get the error that Cannot read property 'confirmPassword' of undefined 我收到Cannot read property 'confirmPassword' of undefined的错误

This leads me to believe that I have no access to the form registrationForm in the controller. 这使我相信我无法访问控制器中的registrationForm表单。 Based on this ( https://docs.angularjs.org/api/ng/type/ngModel.NgModelController ) I imagined that I should. 基于此( https://docs.angularjs.org/api/ng/type/ngModel.NgModelController ),我想应该。

Other solutions that I've seen include passing the form to the controller when the form is submitted, but what I need to do (set custom validation) needs to happen way before that. 我看到的其他解决方案包括在提交表单时将表单传递给控制器​​,但是我需要做的(设置自定义验证)需要在此之前进行。

Other solution mentioned adding the controller to the form via ng-controller but that changed nothing. 其他解决方案提到通过ng-controller将控制器添加到表单中,但没有任何改变。

EDIT: From the website above, is there a reason why in here ( https://plnkr.co/edit/ZT0G6ajdbescT72qLKSU?p=preview ) $scope.myForm can be accessed, but only inside of the $scope.setEmpty function? 编辑:从上面的网站,为什么有一个原因可以在这里( https://plnkr.co/edit/ZT0G6ajdbescT72qLKSU?p=preview )可以访问$ scope.myForm,但是只能在$scope.setEmpty函数内部?

I recommend using the controller itself instead of the $scope provider for this. 我建议为此使用控制器本身而不是$scope提供程序。 This was one of the first issues I came across when working with angularjs 这是我在使用angularjs时遇到的第一个问题

In your controller: 在您的控制器中:

function MyController($scope) {
  var vm = this;
  vm.registrationForm.confirmPassword.$setValidity("PasswordsMatch", $scope.validation.doPasswordsMatch);
}

In your form: 以您的形式:

<form name="vm.registrationForm" ng-submit="registerUser()">
   ...
</form>

The only gotcha is that you need to specify the controllerAs property in your route or ngInclude: 唯一的难题是您需要在路由或ngInclude中指定controllerAs属性:

ng-include="templates/my-template.html" ng-controller="MyController as vm"

or 要么

when('/my-route', {
  templateUrl: 'templates/my-template.html',
  controller: 'MyController as vm'
 })

You need to add a name attribute to form element. 您需要向表单元素添加名称属性。

<form name="registrationForm" ng-submit="registerUser()">
   ...
</form>

this form must rely on a controller indeed. 这种形式确实必须依靠控制器。 please take a look at the example: 请看一个例​​子:

 angular.module("fooApp",[]); angular.module("fooApp") .controller("formctl",function(){ this.user={}; this.dosave=function(){ if(this.user.pwd == this.user.confpwd) alert(this.user.username+" logged"); else alert("pwd does not match"); }; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script> <div ng-app="fooApp"> <h1>sample</h1> <div ng-controller="formctl as ctl"> <form ng-submit="ctl.dosave()"> <label>username</label><br/> <input ng-model="ctl.user.username" required/><br/> <label>password</label><br/> <input ng-model="ctl.user.pwd" type="password" required/><br/> <label>confirm password</label><br/> <input ng-model="ctl.user.confpwd" type="password"/><br/> <input type="submit"/> </form> </div> </div> 

You'll want to pass the form into the submit function to pass the entire form into the controller. 您需要将表单传递给Submit函数,以将整个表单传递给控制器​​。 Use the form name. 使用表格名称。

<form name="registrationForm" ng-submit="registerUser(registrationForm)">
...
</form>

The other option would be to pass the inputs directly in as params 另一个选择是将输入直接作为参数传递

<form name="myForm" novalidate >
  some input: <input type="text" name="sometext" ng-model="myInput">
  <input type="submit" ng-click="doAction(myInput)">
</form>

Quick Plunk https://plnkr.co/edit/s0Al2LTHkvUPoLYNzTpm?p=info 快速Plunk https://plnkr.co/edit/s0Al2LTHkvUPoLYNzTpm?p=info

If you're just after the value of the inputs, it's easier to test if you have explicit parameters that you expect on the method instead of dumping in the entire form. 如果您只是在输入值的后面,那么测试是否具有在方法上期望的显式参数,而不是将其转储为整个表单,将更容易。

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

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