[英]How access a form object not by its name in AngularJS?
I'm using the code below to show the field errors. 我正在使用下面的代码来显示字段错误。 Now I want to reuse the code creating a partial, but I can't find a way to do that without access the form by its name.
现在,我想重用创建部分代码的代码,但是如果不按其名称访问表单,我将找不到一种方法。
There's a way to access the form object without hardcode its name? 有没有一种方法可以访问表单对象而不用硬编码其名称? Or pass that to the partial?
还是将其传递给部分?
Code where the form is registerForm
: 表单为
registerForm
代码:
<div class="error_panel">
<div ng-show="registerForm.$submitted || registerForm.user_name.$touched">
<span ng-show="registerForm.user_name.$error.required">
Por favor, preencha este campo.
</span>
</div>
</div>
Thanks a lot! 非常感谢!
You can use a proxy name for your form that can then be used inside of the partial. 您可以为表单使用代理名称,然后可以在部分内部使用。 For eg something like this.
对于这样的事情。
errors.html
------------
<div class="error_panel">
<div ng-show="thisForm.$submitted || thisForm.user_name.$touched">
<span ng-show="thisForm.user_name.$error.required">
Por favor, preencha este campo.
</span>
</div>
main.html
---------
<form name="registerForm">
.....
<ng-include src="'errors.html'" onload="thisForm=registerForm" />
</form>
This way, the onload attribute on ng-include will get fired when its included and set the current form into the scope, which you can use in your partial. 这样,ng-include上的onload属性将在被包含时触发,并将当前表单设置为作用域,您可以在局部视图中使用它。
Please note that you have field names that are still hardcoded, but the same logic can be applied there as well. 请注意,您的字段名仍然是硬编码的,但是同样的逻辑也可以在此处应用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.