简体   繁体   English

从 Angular 中的另一个组件调用模态对话框的正确方法?

[英]Proper way to call modal dialog from another component in Angular?

I have a component called department where I create a department using a modal dialog as shown below:我有一个名为部门的组件,我在其中使用模式对话框创建一个部门,如下所示:

department.component部门.组件

openModal(data) {
    //code omitted for simplicity
    this.modalService.showMOdal();
    this.create();
}

create() {
    //code omitted for simplicity
}

employee.component员工组件

createDepartment() {
    //???
}

On the other hand, I have another component called employee and I need to create a departmet by calling the open dialog and create methods in the department component.另一方面,我有另一个名为employee 的组件,我需要通过调用打开的对话框来创建一个departmet,并在department 组件中创建方法。

What is the proper way to create department from employee component?从员工组件创建部门的正确方法是什么? Should I implement openModal() and create() methods in employee component as well?我是否也应该在员工组件中实现openModal()create()方法? Or should I call the methods that are already defined in department component?或者我应该调用部门组件中已经定义的方法? I think it sould be better to use already existing methods and components in order to avoid from repetition.我认为最好使用已经存在的方法和组件以避免重复。

Any example approach for this scenario?这种情况的任何示例方法?

Extract this data logic from components and move it to a separate service.从组件中提取此数据逻辑并将其移至单独的服务。

// Move functions for opening the modal from DepartmentComponent to a service
@Injectable({providedIn: 'root'})
export class DepartmentService {
  constructor(private modalService: ModalService){}

  openModal(data) {...}

  create() {...}
}

// Inject the service into EmployeeComponent
export class EmployeeComponent {
  constructur(private departmentService: DepartmentService){}

  createDepartment() {
    this.departmentService.openModal()/create();  // whichever you actually need to call (should probably only be one that delegates to other functions)
  }
}

EDIT:编辑:
With some more information, a specific form (for creating a department) is meant to be displayed in more than one place in the app (in a modal and an employee component).有了更多信息,特定表单(用于创建部门)将显示在应用程序的多个位置(在模式和员工组件中)。

For this, create a component that holds the form (with create button etc) and the required event handlers (eg create department button) and display that where needed (the actual logic for creating the department should be in a separate service).为此,创建一个包含表单(带有创建按钮等)和所需事件处理程序(例如创建部门按钮)的组件,并在需要的地方显示(创建部门的实际逻辑应该在单独的服务中)。

Eg in the employee html例如在员工 html

... employee information ...
<app-createdepartment></app-createdepartment>

And the modal should be something like this (component might have to be in EntryComponents, depending on angular version):模态应该是这样的(组件可能必须在 EntryComponents 中,具体取决于 angular 版本):

let dialogRef = dialog.open(CreateDepartmentComponent, {
  height: '400px',
  width: '600px',
});

(Docs for MatDialog: https://material.angular.io/components/dialog/overview ) (MatDialog 的文档: https://material.angular.io/components/dialog/overview

<button type="button" (click)="addCampaignProduct()" mat-raised-button color="primary"
[title]="'ADD_CAMPAIGN_PRODUCT' | translate:lang">
<i class="material-icons">add_circle</i>{{ 'ADD_CAMPAIGN_PRODUCT' | translate:lang }}
</button>

export class CampaignConfigurableProductsComponent implements OnInit, AfterViewInit { }导出 class CampaignConfigurableProductsComponent 实现 OnInit、AfterViewInit { }


    addCampaignProduct() {
        const dialogRef = this.dialog.open(AddConfigurableProductComponent, {
            disableClose: true,
            data: { campaignId: this.params.id }
        })
        dialogRef.afterClosed().subscribe(() => {
          this.ngOnInit()
        });
    }

export class AddConfigurableProductComponent implements OnInit { 


 addProduct() {
    const selectedOrderIds = this.addProductForm.value.colors
      .map((checked, i) => checked ? this.colorAttributeData[i].config_product_option_value : null)
      .filter(v => v !== null);

    if (this.addProductForm.value.actual_price == '') {
      this.sales_price = this.addProductObj.recommended_price;
    } else {
      this.sales_price = this.addProductForm.value.actual_price;
    }
    this.addProductObj['sales_price'] = this.sales_price;
    this.addProductObj['actual_price'] = this.finalPriceValue;
    this.addProductObj['campaign_id'] = this.data.campaignId;

this.campaignService.addProductCatalog(this.addProductObj).subscribe((response: any) => {
      if (response) { 

      }
  }, (error) => {
      this.notify.error('Something went wrong')
      console.log(error)
    })
}



}

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

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