0

Deal transaction component renders the deals-approval.component, Deal transaction component has the editDeal function ,

How do I call that and use (editRowEvent)="editDeal($event)" on deals-approval.component.html without duplicating the code which is editDeal ?

Thanks.

#deal transaction component

 <div>
    <app-deals-approval  [transaction]="transaction"></app-deals-approval>
  </div>
  <app-table-multi-sort 
       (dataServiceEvent)="dealsServiceEvent($event)" 
       [tableOptions]="tableOptions" 
       [tableData]="data"
       (tableActionsEvent)="tableActions($event)" 
       (editRowEvent)="editDeal($event)">
  </app-table-multi-sort>
</div>

#deal transction ts code

editDeal(deal:any){
    let dealType = '';

    switch(deal.dealType){
      case 'PM Restructure':
        dealType = this.DEAL_TYPES.PMR
        break;
      default:
        break;
    }

    const state = { 
      data: {
        transaction: this.transaction,
        dealId: deal.id,
        dealType: dealType,
        dealName: deal.name,
        breadCrumbsPath:[
          {
            text: 'My transactions',
            link: '/transactions',
          },
          {
            text: this.transaction.name,
            link: `/transactions/overview/${this.transaction.id}`,
          },
        ]
      },      
    }
    this.gotoDealDetails(state);    
  }

#deals-approval.component.html - I wand this (editRowEvent)="editDeal($event)" on deals-approval but the function is on Deal transaction component, how to call that from here?

<div style="padding-bottom: 20px;" [ngClass]="{'hide':!hasApproval}">
    <app-table-multi-sort 
      (editRowEvent)="editDeal($event)" 
      (dataServiceEvent)="dataForApprovalServiceEvent($event)" 
      [tableOptions]="tableOptions"
      [tableData]="data">
    </app-table-multi-sort>
</div>

2 Answers 2

2

Create a service for passing around your $event data to your DealTransactionComponent.

@Injectable({ providedIn: 'root' })
export class AService {
   dataSubject: Subject<any> = new Subject<any>();
}

In your DealsApprovalComponent, inject this service

  constructor(private myService: AService) {}
  
  editDeal($event): void {
    if ($event) {
      this.myService.dataSubject.next($event); // emitting your value
    }
  }

DealTransactionComponent

  // you need a subscription for avoiding memory leaks
  // for that, use ngOnDestroy 
  subscription!: Subscription; 

  constructor(private myService: AService) {}

  ngOnInit(): void {
    this.subscription = this.myService.dataSubject.subscribe((data: any) => {
     this.editDeal(data);
    });
  }
 
  editDeal(deal:any) { .... }

  ngOnDestroy(): void {
    this.subscription?.unsubscribe();
  }
Sign up to request clarification or add additional context in comments.

Comments

0

Add that method to service. Then call it from components Like

public editDeal($event): void {
 this.someService.editDeal($event);
}

6 Comments

what service is this 'someService' ? can you explain
it is some custom service. App to you, such as deal-shared.service.ts
is that an input or output event emitter ? where do I put that function ? in deals approval ?
it is service, you inject it on components where you call editDeal method.
it is just service. See Unrelated Components: Sharing Data with a Service section in medium.com/@nodexperts_88267/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.