简体   繁体   English

无法从一个组件向另一个组件发出函数

[英]not able to emit a function from one component to another component

I have two components in my angular file.我的角度文件中有两个组件。 one is the discussion component and under the discussion component, I created one more component called add-files component.一个是讨论组件,在讨论组件下,我又创建了一个名为 add-files 组件的组件。

I want to emit a function from the discussion component to the add-files component.but I am not able to perform this activity.我想从讨论组件向添加文件组件发出一个函数。但我无法执行此活动。

you can see my efforts below.你可以在下面看到我的努力。 add-files component添加文件组件

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

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

discussion.component.html讨论.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>

discussion.component.ts讨论.component.ts

removeitems1()
{
  alert("called");
  this.sessionService.SetSessionItem("removeUploadfile","true");
  //this.comp.removedoc();
  this.removeitem.emit("hi");
}

you can see from the above code I want to emit the function in the add-files component whenever the user will click on the submit button.您可以从上面的代码中看到,每当用户单击提交按钮时,我都想在 add-files 组件中发出该函数。

the above code is not working and I am new to the Angular.上面的代码不起作用,我是 Angular 的新手。 can anyone suggest something?任何人都可以提出建议吗?

There are two options which you can go for.您可以选择两个选项。 The first one is similiar to your way.第一个和你的方式类似。 The issue is that you pass the event once.问题是你通过了一次事件。 But you want to get triggered each time it is changed:但是您希望每次更改时都被触发:

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

anyFunction(data) {
  // do some code here
}

The second option is to create a service (which I recommend to you).第二种选择是创建一个服务(我向您推荐)。 You can do that by angular-cli (ng gs servicename).你可以通过 angular-cli (ng gs servicename) 来做到这一点。

discussion.service.ts:讨论.service.ts:

export class DiscussionService {

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

  constructor() { }
}

By that, you created a service with an EventEmitter that is available for each component which includes the service.通过这种方式,您创建了一个带有 EventEmitter 的服务,该服务可用于包含该服务的每个组件。 You can read more about EventEmitters here https://angular.io/api/core/EventEmitter .您可以在此处阅读有关 EventEmitter 的更多信息https://angular.io/api/core/EventEmitter To sum it, you just need to emit the event by your component:总而言之,您只需要通过您的组件发出事件:

discussion.component.ts:讨论.component.ts:

constructor(private discussionService: DiscussionService) { }

clickFkt() { this.discussionService.buttonClick.emit(true) }

discussion.component.html:讨论.component.html:

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

Now you can subscribe von every component to your service:现在您可以为每个组件订阅您的服务:

add-files.component.ts:添加-files.component.ts:

constructor(private discussionService: DiscussionService) { }

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

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

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