gpt4 book ai didi

angular - NgbModal.open 在 Angular 4 中不起作用

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

我想使用 angular 4.0.0 和ng-bootstrap 1.0.0-beta.4 但它不显示模态。

应用模块.ts

@NgModule({
// ...
declarations: [
LoginComponent
],
imports: [
// ...
NgbModule.forRoot()
],
entryComponents: [LoginComponent],
})
export class AppModule { }

登录.component.ts

@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent {
@ViewChild('examplemodal')
private modalRef: TemplateRef<any>;

constructor(private modalService: NgbModal) { }

//.... some event that fires onModalRequest()

onModalRequest(): void {
const modalRef = this.modalService.open(this.modalRef); //Not working

modalRef.result.then((result) => {
console.log(result);
console.log('closed');
}).catch( (result) => {
console.log(result);
console.log('cancelling');
});
}
}

examplemodal.html

<ng-template #examplemodal>
<div class="modal">
stuff here...
</div>
</ng-template>

有什么帮助吗?

最佳答案

以下是我在应用程序中的实现方式:

应用程序模块.ts

@NgModule({
// ...
declarations: [
LoginComponent,
ModalComponent
],
imports: [
// ...
NgbModule.forRoot()
],
entryComponents: [ModalComponent], //Only Modal component included here
bootstrap: [AppComponent]
})
export class AppModule { }

我打开模式的组件。 LoginComponent 在你的情况下:

imports ....
import { ModalComponent} from '../Modal/Modal.component'; //My actual Modal component which includes HTML for modal

@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent {
@ViewChild('examplemodal')
private modalRef: TemplateRef<any>;

constructor(private modalService: NgbModal) { }

//.... some event that fires onModalRequest()

onModalRequest(): void {
const modalRef = this.modalService.open(ModalComponent); //Added Modal Component here

modalRef.componentInstance.src = link;//anything u wish to pass to modal component @Input

modalRef.result.then((result) => {
console.log(result);
console.log('closed');
}).catch( (result) => {
console.log(result);
console.log('cancelling');
});

}
}

模态组件.ts:

import { Component, OnInit, Input } from '@angular/core';
import { NgbModal, NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
@Component({
selector: 'app-modal',
templateUrl: './modal.component.html',
styleUrls: ['./modal.component.scss']
})
export class ModalComponent implements OnInit {
@Input() src;
constructor(public activeModal: NgbActiveModal) { }

ngOnInit() {
}

}

Modal.component.html:

<div class="modal-header">
<button type="button" class="close" aria-label="Close" (click)="activeModal.dismiss('Cross click')">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
STUFF HERE
</div>

在这里工作 Demo

请注意:您需要为 SCSS 安装 Bootstrap 4。npm install bootstrap -D 并将其引用包含在您的 style.scss 中,例如:

@import "node_modules/bootstrap/scss/bootstrap";

关于angular - NgbModal.open 在 Angular 4 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49244566/

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