简体   繁体   English

为 Angular 模板元素引用赋值的目的是什么?

[英]Purpose of assigning a value to the Angular template element reference?

I noticed that some devs assign a value to their element reference like this:我注意到一些开发人员为他们的元素引用分配了一个值,如下所示:

#searchInput="matInput"

And this has the effect of turning off autocomplete for the field.这具有关闭该字段的自动完成功能的效果。 Is that why it's done?这就是它完成的原因吗?

This is a more complete example:这是一个更完整的例子:

<mat-form-field floatLabel="never">
  <input matInput #searchInput="matInput" type="text" placeholder="Search"/>
</mat-form-field>

This article has more context . 这篇文章有更多的上下文 It uses the #searchInput in a declarative way only.它仅以声明方式使用#searchInput It will work without assigning a value to #searchInput which made me curious about why people do assign a value to it.它可以在不为#searchInput赋值的情况下工作,这让我很好奇人们为什么要为它赋值。

No, it' completely unrelated.不,这完全不相关。

This syntax is used to get the Angular component as a view child of your current component.此语法用于获取 Angular 组件作为当前组件的视图子项。

In the source code, it's defined in the @Component decorator as exportAs .在源代码中,它在@Component装饰器中定义为exportAs

(See it on their repo) (见他们的回购)

It allows a component to declare a variable as a material input, and control it programmatically.它允许组件将变量声明为材质输入,并以编程方式对其进行控制。

To turn the autocomplete off, this is totally different.要关闭自动完成功能,这是完全不同的。 Since I'm not sure if you are asking for that of for someone to explain the syntax, I'll simply stop there and wait for you to comment my answer, in case you need it !由于我不确定您是否要求某人解释语法,所以我会简单地停在那里等待您评论我的答案,以防您需要它!

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

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