Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 无法将函数从一个组件发送到另一个组件_Angular_Event Handling_Angular8 - Fatal编程技术网

Angular 无法将函数从一个组件发送到另一个组件

Angular 无法将函数从一个组件发送到另一个组件,angular,event-handling,angular8,Angular,Event Handling,Angular8,我的角度文件中有两个组件。 一个是讨论组件,在讨论组件下,我又创建了一个名为addfiles组件的组件 我想将一个函数从讨论组件发送到添加文件组件。但我无法执行此活动 你可以在下面看到我的努力。 添加文件组件 @Input('removeFile') removeAttachments: boolean = false; removeitem($event: string) { alert("in file component"); this.uploader.clearQueue

我的角度文件中有两个组件。 一个是讨论组件,在讨论组件下,我又创建了一个名为addfiles组件的组件

我想将一个函数从讨论组件发送到添加文件组件。但我无法执行此活动

你可以在下面看到我的努力。 添加文件组件

@Input('removeFile')
  removeAttachments: boolean = false;

removeitem($event: string)
{
  alert("in file component");
  this.uploader.clearQueue();
}
discussion.component.html

<div class="col-12 col-sm-6 col-lg-6">
            <input type="submit" value="Send" (click)="removeitems1()" class="greenBtn float-right addCommentsBtn"
              [disabled]="commentForm.pristine || commentForm.invalid" onclick="closeCommnetPopupBox()">
          </div>

<app-add-files  (removeitems)="removeitem($event)"></app-add-files>
从上面的代码可以看出,每当用户单击submit按钮时,我都希望在AddFiles组件中发出函数


上面的代码不工作,我是新的角度。有人能提出建议吗?

有两种选择,你可以选择。第一个与你的方式相似。问题是你只通过一次活动。但您希望在每次更改时被触发:

@Input() set removeFile(data: boolean) { this.anyFunction(data); }

anyFunction(data) {
  // do some code here
}
第二个选项是创建一个服务(我向您推荐)。您可以通过angular cli(ng g s servicename)来实现这一点

discussion.service.ts:

export class DiscussionService {

  buttonClick: EventEmitter<boolean> = new EventEmitter();

  constructor() { }
}
discussion.component.html:

<input (click)="clickFkt()">

removeitem EventEmitter在哪里?您应该使用属性绑定
[]
而不是
中的
()
,因为您想从
讨论中发送数据。component
添加文件。component
@NicholasK我尝试了您的方法,但它在我的代码中给出了一个错误。我想我们需要stackbllitz来进一步帮助。还有,您得到了什么错误?还有,为什么将
@Input
放在
removeAttachments
上?它不应该在
removietem(…)上吗?
constructor(private discussionService: DiscussionService) { }

clickFkt() { this.discussionService.buttonClick.emit(true) }
<input (click)="clickFkt()">
constructor(private discussionService: DiscussionService) { }

ngOnInit() {
   this.discussionService.buttonClick.subscribe(click => {
       this.anyFunction(click);
   });
}
anyFunction(data) {
   // do some code here
}