简体   繁体   English

如何以角度访问子组件到父组件的功能

[英]How to access function from child component to parent component in angular

Trying to access the remove function from child component(chips-autocomplete-example).尝试从子组件(chips-autocomplete-example)访问删除功能。 But not able to access it in app.component.但无法在 app.component 中访问它。 When i click the close (X) icon, i want to get the value of that.当我单击关闭 (X) 图标时,我想获得它的值。 For example, if i click the Apple close icon, i want to get the Apple text in the alert.例如,如果我单击 Apple 关闭图标,我想在警报中获取 Apple 文本。 Same like for others(Lemon, Lime, Orange, Strawberry) I am doing this from the app.component ts file.和其他人一样(柠檬、酸橙、橙子、草莓)我是从 app.component ts 文件中执行此操作的。 But not working.但不工作。 How to resolve this?如何解决这个问题?

app.component.ts: app.component.ts:

  removeClick(removeval) {
    alert(removeval);
  }

app.component.html: app.component.html:

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

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

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>();不确定我是否正确理解了您的问题,但是从孩子做父母(以最简单的方式)您可以使用@Output() removed = new EventEmitter<string>(); and in remove function add this.removed.emit(fruit) in child and then in parent并在删除功能中添加this.removed.emit(fruit)在子项中,然后在父项中

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

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

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