0

我想使用服务将一组员工 ID 发送到另一个组件。我有 2 个组件 - component1 和 component2。从 component1 我正在使用复选框选择员工。这是我的 component.ts 文件,我从中将员工 ID 数组发送到服务。

component1.ts 文件

      checkbox_selected = []
      ischecked: boolean;
     constructor(private attendanceService: EmployeeService, private router: Router,
     private fb: FormBuilder) {  }

     onChange(checkbox: any, isChecked: boolean) {
     console.log("checkbox")
     this.checkbox_selected.push(checkbox)
     this.id = checkbox
     this.ischecked = isChecked
     console.log(this.checkbox_selected)
     }
    show(){
    if(this.ischecked == true){
   this.router.navigate(["./component2"])
   this.attendanceService.my_employee_id(this.checkbox_selected);

    }

服务.ts 文件

     export class EmployeeService {
      my_employee_id(id) {
      console.log(id)
     return this.emp_id;
     }
     }

在我的服务文件中,我正在获取员工 ID 数组。如何在另一个组件中获取这些员工 ID 数组?

4

1 回答 1

0

请在询问之前阅读角度文档或做一些研究。请阅读有关使用服务的 Angular 组件交互

在高层次上,你想要的是...... [见下文]

服务

import { Injectable } from '@angular/core';
import { Subject }    from 'rxjs';

@Injectable({
    providedIn: 'root'
})
export class EmployeeService {

  // Observable string sources
  private selectedEmpSub = new Subject<string[]>();

  // Observable string streams
  public myEmpSubj$ = this.selectedEmpSub.asObservable();

  // Service methods
  myEmployeeId(empIds: string[]) {
    this.selectedEmpSub.next(empIds);
    this.selectedEmpSub.complete();
  }

}

组件一

this.attendanceService.myEmployeeId(this.checkbox_selected);

组件-2

this.attendanceService.myEmpSubj$.subscribe(empId => {
    this.empId = empId;
    console.log(this.empId);
});
于 2020-03-17T06:31:54.243 回答