如何在角度访问从子组件到父组件的函数

发布于 2025-02-12 00:02:02 字数 638 浏览 2 评论 0 原文

尝试从儿童组件(Chips-autocomplete-示例)访问删除函数。但无法在app.component中访问它。当我单击关闭(x)图标时,我想获得它的值。例如,如果我单击Apple Close Icon,我想将Apple文本列入警报。与其他人一样(柠檬,酸橙,橙色,草莓),我正在app.component ts文件中这样做。但不起作用。如何解决这个问题?

app.component.ts:

  removeClick(removeval) {
    alert(removeval);
  }

app.component.html:

<chips-autocomplete-example
  (removed)="removeClick(fruit)"
></chips-autocomplete-example>

demo:

Trying to access the remove function from child component(chips-autocomplete-example). But not able to access it in app.component. When i click the close (X) icon, i want to get the value of that. For example, if i click the Apple close icon, i want to get the Apple text in the alert. Same like for others(Lemon, Lime, Orange, Strawberry) I am doing this from the app.component ts file. But not working. How to resolve this?

app.component.ts:

  removeClick(removeval) {
    alert(removeval);
  }

app.component.html:

<chips-autocomplete-example
  (removed)="removeClick(fruit)"
></chips-autocomplete-example>

Demo: https://stackblitz.com/edit/angular-sbrx2p-k89gvp?file=app.component.ts

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

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

发布评论

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

评论(1

紫瑟鸿黎 2025-02-19 00:02:02

不确定我是否正确理解您的问题,但是从孩子那里做父母(以最简单的方式)您可以使用
@Output()removed = new EventEmitter&lt; string&gt;(); 和在删除函数中添加 this.remeved.emit.emit(frual)(frual)(frual) in Child,然后在父母中

<chips-autocomplete-example
  (removed)="removeClick($event)"
></chips-autocomplete-example>

Not sure if I'm understand your problem correctly, but from child do parent (in the easiest way) you can use
@Output() removed = new EventEmitter<string>(); and in remove function add this.removed.emit(fruit) in child and then in parent

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