首页 文章

动态更改Angular 5 ng-bootstrap模态对话框的模板

提问于
浏览
3

我正在开发一个模态对话框,我需要能够动态地动态更改templateURL . 显示的是默认模板 . 我只是想知道如何实现它,因为这将通过templateURL名称和位置传入调用 . 下面是我的组件代码:

import { ModalService } from './../../services/modal.service';
import {Component, Input} from '@angular/core';

import {NgbModal, NgbActiveModal} from '@ng-bootstrap/ng-bootstrap';

@Component({
  selector: 'app-ngbd-modal-content',
  templateUrl: '../../templates/modal.tp1.html'
})

export class ModalOptionsComponent {
  @Input() name;

  constructor(public activeModal: NgbActiveModal, modelService: ModalService) {}
}

export class  NgbdModalComponent {
  constructor(private ngbModal: NgbModal) {}
}

理想情况下,我想从下面的服务类和组件中打开它,但我不确定如何做到这一点 . 我已经做了相当多的研究,但我对如何实现这一点并没有太多了解 .

Modal.service.ts:

import { ModalOptionsComponent } from './../pages/modal-options/modal-options.component';
import { Injectable } from '@angular/core';
import {NgbModal, NgbActiveModal} from '@ng-bootstrap/ng-bootstrap';

@Injectable()
export class ModalService {

  constructor(private ngbModal: NgbModal, private modalComp: ModalOptionsComponent) { }

  modalService(modal: any) {
    const modalDefaults = {
      templateUrl: '../templates/modal.tp1.html'
    };
    const modalOptions = {
      hasClose: true,
      closeButtonText: 'CLose',
      actionButtonText: 'OK',
      headerText: 'Proceed?',
      bodyText: 'Perform this action?',
      okResult: {}
    };
  }

  showModal(customModalDefaults: any, cusomeModalOptions: any) {

  }


}

我需要做的一件事就是为此创建服务类,我对角度很新,并想知道如何实现这一点 .

2 回答

  • 1

    因为我无法找到更改templateUrl的能力,我必须为我需要的每种类型的对话框创建一个组件 . 这方面的缺点是它不再是动态的,但它适合我需要它 . 基本上,您可以使用该服务生成所需的组件类型 . 有一件事是不幸的是,使用动态组件加载并不是真正可行的解决方案,因为这样做很复杂 . 以下是我所做的一个例子 .

    Component code for default modal dialog:

    import {Component, Input} from '@angular/core';
    
    import {NgbModal, NgbActiveModal} from '@ng-bootstrap/ng-bootstrap';
    
    @Component({
      selector: 'app-ngbd-modal-content',
      template: `
    <div class="modal-header">
    <h3 class="font-32" style="padding-bottom: 0px">{{headerText}}</h3>
    </div>
    <div class="modal-body">
    <p>{{bodyText}}</p>
    </div>
    <div class="modal-footer">
    <button type="button" class="btn btn-outline-dark" (click)="activeModal.close('Close click')">Close</button>
    </div>
      `,
      styleUrls: ['./default-modal-dialog.component.scss']
    })
    export class DefaultModalDialogComponent {
      @Input() bodyText;
      @Input() headerText;
    
      constructor(public activeModal: NgbActiveModal) {}
    
    }
    

    Component code for FeaturedSearch:

    import {Component, Input} from '@angular/core';
    
        import {NgbModal, NgbActiveModal} from '@ng-bootstrap/ng-bootstrap';
    
        @Component({
        selector: 'app-featured-search-dialog',
        template: `
        <div>
            <div class="modal-header">
                <h3 class="font-32" style="margin-bottom: -16px;">Edit Heading and Subheading</h3>
            </div>
            <div class="modal-body" style="padding:30px;">
                <div class="row">
                    <div class="col-md-12">
                        <label class="font-14-bold">Heading</label>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-12">
                        <input style="box-shadow: rgb(102, 102, 102) 0px 0px 4px inset; border: 0px; margin-bottom: 50px;"  type="text" class="form-control input-sm" ng-model="modalOptions.okResult.heading" >
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-12">
                        <label class="font-14-bold">Subheading</label>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-12">
                        <input style="box-shadow: rgb(102, 102, 102) 0px 0px 4px inset; border: 0px;" type="text" class="form-control input-sm" ng-model="modalOptions.okResult.subheading" >
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <div style="margin-top: 8px; margin-bottom: 8px;">
                    <button style="background-color: #999999; color: #ffffff;" type="button" ng-show="modalOptions.okResult.buttonText !== 'Close'"
                            class="btn font-14-bold"
                            data-ng-click="modalOptions.close()">Close
                    </button>
                    <button style="background-color: #004065; color: #ffffff; width: 70px;" type="button" class="btn ng-binding" ng-disabled="modalOptions.okResult.heading.length <= 0 || modalOptions.okResult.subheading.length <=0" data-ng-click="modalOptions.ok()">OK</button>
                </div>
            </div>
        </div>
        `,
        styleUrls: ['./featured-search-dialog.component.scss']
        })
        export class FeaturedSearchDialogComponent {
    
        @Input() heading;
        @Input() subheading;
    
        constructor(public activeModal: NgbActiveModal) {}
    
        }
    

    Custom Modal Service :

    import { Injectable, Input, ComponentFactoryResolver } from '@angular/core';
    import {NgbModal, NgbActiveModal} from '@ng-bootstrap/ng-bootstrap';
    @Injectable()
    export class ModalService {
        constructor(private ngbModal: NgbModal,
                    private componentFactoryResolver: ComponentFactoryResolver) {}
    
      showDefaultModalComponent(theComponent: any, headerText: any, bodyText: any) {
        const componenetFactory = this.componentFactoryResolver.resolveComponentFactory(theComponent);
        const modalRef = this.ngbModal.open(theComponent);
        modalRef.componentInstance.bodyText = bodyText;
        modalRef.componentInstance.headerText = headerText;
        return modalRef;
      }
    
      showFeaturedDialog(theComponent: any, heading: any, subheading: any) {
        const componenetFactory = this.componentFactoryResolver.resolveComponentFactory(theComponent);
        const modalRef = this.ngbModal.open(theComponent);
        return modalRef;
      }
    
    
    }
    
  • 0

    首先:NgbModal,NgbActiveModal来自包'@ ng-bootstrap / ng-bootstrap' .

    我认为没有必要创建单独的服务,因为“动态模板”功能可以通过包本身提供的功能来实现 . 我们来看看如何:

    请参考下面的图片(我猜's the scenario). In your main component where you'已经把你的'default-modal',在我的情况下它是-detail(文件夹),从该组件(-detail.component)触发模态(默认模态) .

    假设您正在通过单击按钮编辑* -detail.component中的事件 . 此单击会触发“默认模态”弹出窗口 . 我们在代码中看到这一点 .

    *-.detail.component.html <button class="btn" (click)="editEvents(eventvalues)"> Edit </button>

    *-detail.component.ts

    editEvents(events) {
    const activeModal = this.modalService.open(DefaultModal, { size: 'lg' });
    activeModal.componentInstance.modalHeader = 'Edit Event';
    activeModal.componentInstance.eventdata = events;  }
    

    现在,模态组件: modal.component.html

    <p>{{modalHeader}}</p>
    <p> {{eventdata }} </p>
    

    请注意,您需要对文件进行必要的导入 . 在组件模块导入中,NgbModalModule也会添加到imports数组中 . 在component.ts文件中:NgbModal并从'./default-modal/default-modal.component'导入;

    enter image description here

相关问题