gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 19:26:10 49 4
gpt4 key购买 nike

我正在开发一个模态对话框,我需要能够动态地动态更改 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) {}
}

理想情况下,我想从下面的服务类而不是组件打开它,但我不太确定该怎么做。我已经做了相当多的研究,但我没有找到太多关于如何实现这一点的信息。

模态服务.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 还很陌生,想知道如何实现这一目标。

最佳答案

因为我找不到更改 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) {}

}

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) {}

}

自定义模式服务:

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;
}


}

关于angular - 动态更改 Angular 5 ng-bootstrap 模态对话框的模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50163277/

49 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com