简体   繁体   English

角度2模板参考变量

[英]angular 2 template reference variable

i have a little issue with understanding template variable reference, when used with NgForm, here is what in the angular2 doc : 与NgForm一起使用时,我对理解模板变量引用有一点问题,这是angular2 doc中的内容:

<form (ngSubmit)="onSubmit(theForm)" #theForm="ngForm">
  <button type="submit" [disabled]="!theForm.form.valid">Submit</button>
</form>

What is the value of theForm? 表格的价值是什么?

It would be the HTMLFormElement if Angular hadn't taken it over. 如果Angular没有接管它,它将是HTMLFormElement。 It's actually ngForm, a reference to the Angular built-in NgForm directive that wraps the native HTMLFormElement and endows it with additional superpowers such as the ability to track the validity of user input. 它实际上是ngForm,它是对Angular内置NgForm指令的引用,该指令包装了本地HTMLFormElement并赋予它附加的超能力,例如能够跟踪用户输入的有效性。

i understand what theForm value, but i don't get from where we get the ngForm variable, is it a property of the NgForm directive? 我知道theForm的值是什么,但是我从哪里获得ngForm变量,但它不是NgForm指令的属性?

exportAs - name under which the component instance is exported in a template exportAs-在模板中导出组件实例的名称

The ngForm is the Angular form directive. ngForm是Angular表单指令。

You can expose your directive to the view as local variables with the exportAs key. 您可以使用exportAs指令作为局部变量公开给视图。

From the source code: 从源代码:

@Directive({
  selector: 'form:not([ngNoForm]):not([formGroup]),ngForm,[ngForm]',
  exportAs: 'ngForm'
  ...
})

Now you can access the ngForm directive API in your template as ngForm. 现在,您可以以ngForm的形式访问模板中的ngForm指令API。

When you write this: 当您编写此代码时:

#theForm="ngForm"

What it does is create a local variable to your template and assign it's value to the form directive. 它的作用是为模板创建一个局部变量,并将其值分配给form指令。

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

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