[英]Angular2 ng-bootstrap modal components
我有一个使用ng-bootstrap创建的模态组件 ,如follow(只是一个正文):
<template #content let-c="close" let-d="dismiss">
<div class="modal-body">
<p>Modal body</p>
</div>
</template>
它是角度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);
});
}
}
我真的希望能够从我网站上的其他组件调用此模式。 我只想从我的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. **/
}
}
有人可以解释我该怎么做? 我来自棱角1.x,它简单得多......
这就是我使用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() {
}
}
模板将是这样的:
<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>
在要打开模态的组件中,添加NgbModal类型的变量并调用open,如下所示:
export class MyComponent {
constructor(private modal: NgbModal) {}
onClick() {
this.modal.open(CreateAssetModalComponent);
}
}
在声明CreateAssetModalComponent的NgModule中,在entryComponents数组中添加组件,如下所示:
@NgModule({
imports: [...],
declarations: [CreateAssetModalComponent],
entryComponents: [CreateAssetModalComponent]
})
假设您有其他模块,如NgbModule导入,这应该工作。
我不确定我是否完全理解你想要做什么,但实质上它很简单 - 打开一个带有特定内容的模态窗口,你只需要在NgbModal
服务上调用open
方法。 最简单的实现可能是单行( this.modalService.open('Hi tehre!');
):
@Component({
selector: 'my-home',
templateUrl: 'src/modal-basic.html'
})
export class HomeComponent implements OnInit {
constructor(private modalService: NgbModal) {}
ngOnInit(content) {
this.modalService.open('Hi tehre!');
}
}
在plunker中直播: http ://plnkr.co/edit/5qq04Q4HFLOe9tsyeMMI?p=preview
它与AngularJS 1.x的angular-ui / bootstrap 完全相同 ! 原则完全一样,没有任何改变。
可能是什么让你头疼的是如何指定的模式的内容 。 在上面的例子中我使用了一个字符串但是在绝大多数情况下你想要使用带有绑定,指令等的HTML。但是Angular是不同的,因为你不想传递HTML字符串,如果你不想将编译器发送到生产(你真的不想这样做......)。
所以你的下一个最佳选择是使用另一个组件作为内容,这是一个最小的例子: http : //plnkr.co/edit/EJyZ6nF5WVAS9bvQycQe?p = preview
注意 :由于Angular本身存在错误,您需要使用setTimeout
包装open()
方法调用。 错误参考: https : //github.com/angular/angular/issues/15634
将HelloHomeModalComponent
添加到构造函数参数中,并从HomeComponent调用open
函数,如下所示:
import { Component, OnInit } from '@angular/core';
import { HelloHomeModalComponent } from "hello-home-modal.component";
@Component({
selector: 'my-home',
templateUrl: './home.component.html'
})
export class HomeComponent implements OnInit {
constructor(private modal: HelloHomeModalComponent) {}
ngOnInit() {
this.modal.open();
}
}
如果使用@ViewRef()从模态组件获取适当的模板引用,则无需将content
参数传递给组件的open()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.