Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在Angular中,如何在控制器中获取$scope中的表单?_Javascript_Angularjs_Angularjs Scope - Fatal编程技术网

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

Javascript 在Angular中,如何在控制器中获取$scope中的表单?,javascript,angularjs,angularjs-scope,Javascript,Angularjs,Angularjs Scope,拥有这两个文件: HTML: 我得到一个错误,无法读取未定义的属性“confirmPassword” 这使我相信我无法访问控制器中的表单registrationForm。基于这个()我认为我应该 我见过的其他解决方案包括在提交表单时将表单传递给控制器,但我需要做的(设置自定义验证)必须在提交表单之前完成 另一个解决方案提到通过ng controller将控制器添加到表单中,但这并没有改变什么 编辑: 从上面的网站来看,是否有理由可以访问in here()$scope.myForm,但只能在$sc

拥有这两个文件:

HTML:

我得到一个错误,
无法读取未定义的属性“confirmPassword”

这使我相信我无法访问控制器中的表单
registrationForm
。基于这个()我认为我应该

我见过的其他解决方案包括在提交表单时将表单传递给控制器,但我需要做的(设置自定义验证)必须在提交表单之前完成

另一个解决方案提到通过
ng controller
将控制器添加到表单中,但这并没有改变什么

编辑:
从上面的网站来看,是否有理由可以访问in here()$scope.myForm,但只能在
$scope.setEmpty
函数中访问?

您需要向表单元素添加name属性

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

...

我建议使用控制器本身,而不是
$scope
提供程序。这是我在与angularjs合作时遇到的第一个问题之一

在控制器中:

$scope.registrationForm.confirmPassword.$setValidity("PasswordsMatch", $scope.validation.doPasswordsMatch);
function MyController($scope) {
  var vm = this;
  vm.registrationForm.confirmPassword.$setValidity("PasswordsMatch", $scope.validation.doPasswordsMatch);
}
以您的形式:

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


此表单必须依赖于控制器。请看一个例子:

angular.module(“fooApp”,[]);
角度模块(“fooApp”)
.controller(“formctl”,函数(){
this.user={};
this.dosave=function(){
if(this.user.pwd==this.user.confpwd)
警报(this.user.username+“已记录”);
其他的
警报(“pwd不匹配”);
};
});

样品
用户名

密码

确认密码


您需要将表单传递到submit函数中,以将整个表单传递到控制器中。使用表单名称

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

...
另一个选项是将输入作为参数直接传入

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

一些输入:
快速撞击


如果您只是在输入值之后,则更容易测试您是否在方法上具有预期的显式参数,而不是在整个表单中转储。

应该是
,name是表单标记的有效属性对不起,这最初是
name
而不是
表单
。刚刚修正了这个问题。它原来是
名称
,一定是在写问题时意外更改的。刚刚修复了它,谢谢你指出。我们为什么要使用变量
vm
?我们可以这样做吗?
注册表单…
?此外,我已经完成了代码片段#1、#2和#4中的步骤(我在路由器中设置了控制器),但仍然会出现相同的错误。在引用模板时,必须确保使用了
控制器
,否则它将无法工作。您可能可以使用
,但这是一种不好的做法,因为
是上下文,并且根据使用位置的不同而改变含义您不必使用
vm
,但它是首选名称。查看此样式指南:我设置控制器的行是
。。。控制器:“注册控制器为vm”…
这不是您设置它的方式吗controllerAs?这就是我现在正在做的,但我想实现实时验证,而不是只有当用户提交表单时才会发生的验证。@SammyI。看看这是否适合你。甚至还有一个砰的一声。
when('/my-route', {
  templateUrl: 'templates/my-template.html',
  controller: 'MyController as vm'
 })
<form name="registrationForm" ng-submit="registerUser(registrationForm)">
...
</form>
<form name="myForm" novalidate >
  some input: <input type="text" name="sometext" ng-model="myInput">
  <input type="submit" ng-click="doAction(myInput)">
</form>