配对或连接输入和按钮元素与角元素

发布于 2025-02-03 22:18:07 字数 760 浏览 1 评论 0原文

我正在关注教程英雄之旅添加一个新英雄他们说

您可以使用与添加按钮配对的元素。
标题之后,将以下内容插入英雄组合模板中:

<div>
  <label for="new-hero">Hero name: </label>
  <input id="new-hero" #heroName />

  <!-- (click) passes input value to add() and then clears the input -->
  <button type="button" class="add-button" (click)="add(heroName.value); heroName.value=''">
    Add hero
  </button>
</div>

在这里,我不了按钮元素。

基本上,该输入元素中的#####&lt;语法。我知道这不是已经声明的id

I was following the tutorial Tour of Heroes. While adding a new hero they say

You can use an element paired with an add button.
Insert the following into the HeroesComponent template, after the heading:

<div>
  <label for="new-hero">Hero name: </label>
  <input id="new-hero" #heroName />

  <!-- (click) passes input value to add() and then clears the input -->
  <button type="button" class="add-button" (click)="add(heroName.value); heroName.value=''">
    Add hero
  </button>
</div>

Here I don't understand what is #heroName inside in input element (what is it called) and how does it help in pairing that input element with the button element.

Basically, what is that #<keyword> syntax within that input element. I know that it is not the id as that is already declared.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

灵芸 2025-02-10 22:18:08

要回答问题,它是对输入的参考。您可以在此处找到更多详细信息:

https://angular.io/guide/guide/template-template-reference-variables-variables-

模板变量可帮助您使用来自模板一部分的数据
模板的另一部分。使用模板变量执行任务
例如响应用户输入或细微调整您的应用程序表格。

在教程上下文中,它是对输入元素的引用。它有助于将其与按钮配对,以便能够访问其值,而无需实际定义组件中的变量并尝试直接更新模板。这有助于您“跳过”步骤,并且实际上可以直接访问该值。

在某些情况下,模板参考变量可能会非常方便,例如

<mat-menu #menuComponent ...></mat-menu>
<button (click)="menuComponent.close()"></button>

在上面的示例中通常在Angular材料中使用(为组件调用功能),您将Menucompents变量绑定到“ Mat-Menu”组件,在这种情况下,您可以访问所有变量,公共方法。在这种情况下,我们可以从MAT-MENU组件中调用“关闭”方法。

让我知道这是否仍然不清楚,我可以尝试为您提供更多示例和解释

To answer the question, it's a reference to the input. You can find more details here:

https://angular.io/guide/template-reference-variables

Template variables help you use data from one part of a template in
another part of the template. Use template variables to perform tasks
such as respond to user input or finely tune your application's forms.

In the tutorial context, it's a reference to the input element. It helps to pair it with a button to be able to access it's value, without having to actually define a variable in the component.ts and trying to update the template directly. This help you "skip" a step, and actually have direct access to that value.

Template reference variables can become very handy in certain cases and are commonly used for example in angular material ( to call a function for a component )

<mat-menu #menuComponent ...></mat-menu>
<button (click)="menuComponent.close()"></button>

In the above example, you bind the menuComponent variable to "mat-menu" component, in which case you can access all the variables, public methods of such. In that case we can call "close" method from the mat-menu component.

Let me know if this is still unclear and I can try to give you more examples and explanation

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文