我正在处理一个模态对话框,我需要能够动态地动态更改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) {
}
}
我需要做的一件事就是为此创建服务类,我对角度很新,并想知道如何实现这一点。
答案 0 :(得分:1)
因为我无法找到更改templateUrl的能力,所以我必须为我需要的每种类型的对话框创建一个组件。这样做的缺点是它不再是动态的,但它适合我需要的东西。基本上,您可以使用该服务生成所需的组件类型。有一件事是不幸的是,使用动态组件加载并不是一个真正可行的解决方案,因为这样做很复杂。以下是我所做的一个例子。
默认模式对话框的组件代码:
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) {}
}
特色搜索的组件代码:
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) {}
}
自定义模态服务:
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;
}
}
答案 1 :(得分:0)
首先:NgbModal,NgbActiveModal来自'@ ng-bootstrap / ng-bootstrap'包。
我认为没有必要创建单独的服务,因为“动态模板”功能可以通过包本身提供的功能来实现。我们来看看如何:
参考下面的图片(我猜这是场景)。在您放置'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'导入{DefaultModal};