动态更改Angular 5 ng引导模式对话框的模板
我正在处理一个模式对话框,我需要能够动态地更改templateURL以进行动态更改。显示的是默认模板。我只是想知道如何实现这一点,因为传入templateURL名称和位置时会调用它。下面是我的组件代码:动态更改Angular 5 ng引导模式对话框的模板,angular,typescript,ng-bootstrap,Angular,Typescript,Ng Bootstrap,我正在处理一个模式对话框,我需要能够动态地更改templateURL以进行动态更改。显示的是默认模板。我只是想知道如何实现这一点,因为传入templateURL名称和位置时会调用它。下面是我的组件代码: import { ModalService } from './../../services/modal.service'; import {Component, Input} from '@angular/core'; import {NgbModal, NgbActiveModal} fr
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) {
}
}
我需要做的一件事就是为此创建服务类。我对angular非常陌生,不知道如何实现这一点。首先:NgbModal,NgbActiveModal来自包'@ng bootstrap/ng bootstrap' 我认为您不需要创建单独的服务,因为“动态模板”功能可以通过软件包本身提供的内容来实现。让我们看看如何: 请参考下图(我想这就是场景)。在您放置“默认模式”的主组件中,在我的示例中是-detail(文件夹),模式(默认模式)是从该组件(-detail.component)触发的 假设您正在通过单击按钮在*-detail.component中编辑事件。此单击将触发“默认模式”弹出窗口。让我们在代码中看到这一点 *-.detail.component.html
编辑
*-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>
{{modalHeader}
{{eventdata}}
请注意,您需要对文件进行必要的导入。
在组件模块导入中,NgbModalModule还添加到导入阵列。
在component.ts文件中:NgbModal并从“./default-modal/default-modal.component”导入{DefaultModal}
因为我找不到改变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) {}
}
我在我的应用程序中这样做,而不是创建不同的templateURL,我只是使用*ngIf来分隔要显示的内容。您还可以在组件中使用变量,这将有条件地更改任何文本。祝你好运@不幸的是,我已经创建了.html,我必须使用它。我还可以将模板放在精简的html组件中,并有条件地在通用模式模板中调用它们。你能这样做吗?或者你是在坚持这样做吗?将每个模板构建为自己的组件,然后根据需要动态加载它们@JC福特我同意
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;
}
}