use*_*839 7 bootstrap-modal ng-bootstrap angular
我有一个使用ng-bootstrap创建的模态组件,如follow(只是一个正文):
<template #content let-c="close" let-d="dismiss">
<div class="modal-body">
<p>Modal body</p>
</div>
</template>
Run Code Online (Sandbox Code Playgroud)
它是角度2组件
import { Component } from '@angular/core';
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
@Component({
selector: 'my-hello-home-modal',
templateUrl: './hellohome.modal.html'
})
export class HelloHomeModalComponent {
closeResult: string;
constructor(private modal: NgbModal) {}
open(content) {
this.modal.open(content).result.then((result) => {
this.closeResult = `Closed with: ${result}`;
}, (reason) => {
console.log(reason);
});
}
}
Run Code Online (Sandbox Code Playgroud)
我真的希望能够从我网站上的其他组件调用此模式.我只想从我的homeComponent调用open方法.
看我的homeComponent
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'my-home',
templateUrl: './home.component.html'
})
export class HomeComponent implements OnInit {
constructor() {
}
ngOnInit() {
console.log('Hello Home');
/** want call modal popin here from the init component method in order to test the modal. **/
}
}
Run Code Online (Sandbox Code Playgroud)
有人可以解释我该怎么做?我来自棱角1.x,它简单得多......
小智 19
这就是我使用Angular 5和ng-bootstrap的方法.创建模态组件,如下所示:
import { Component, OnInit } from '@angular/core';
import {NgbActiveModal} from '@ng-bootstrap/ng-bootstrap';
@Component({
selector: 'sm-create-asset-modal',
templateUrl: './create-asset-modal.component.html',
styleUrls: ['./create-asset-modal.component.css']
})
export class CreateAssetModalComponent implements OnInit {
constructor(public activeModal: NgbActiveModal) { }
ngOnInit() {
}
}
Run Code Online (Sandbox Code Playgroud)
模板将是这样的:
<div class="modal-header">
<h4 class="modal-title">Create New </h4>
<button type="button" class="close" aria-label="Close" (click)="activeModal.dismiss('Cross click')">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>One fine body…</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-success" (click)="activeModal.close('Close click')">Create</button>
</div>
Run Code Online (Sandbox Code Playgroud)
在要打开模态的组件中,添加NgbModal类型的变量并调用open,如下所示:
export class MyComponent {
constructor(private modal: NgbModal) {}
onClick() {
this.modal.open(CreateAssetModalComponent);
}
}
Run Code Online (Sandbox Code Playgroud)
在声明CreateAssetModalComponent的NgModule中,在entryComponents数组中添加组件,如下所示:
@NgModule({
imports: [...],
declarations: [CreateAssetModalComponent],
entryComponents: [CreateAssetModalComponent]
})
Run Code Online (Sandbox Code Playgroud)
假设您有其他模块,如NgbModule导入,这应该工作.
| 归档时间: |
|
| 查看次数: |
18137 次 |
| 最近记录: |