我创建了一个modal component ng-bootstrap跟随(只是一个正文):
<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,它简单得多......
答案 0 :(得分:17)
这是我使用Angular 5和ng-bootstrap的方法。按如下方式创建模态组件:
createInitialLayout
,模板将类似于:
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() {
}
}
在要打开模态的组件中添加NgbModal类型的变量并按如下方式调用open:
<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>
在声明CreateAssetModalComponent的NgModule中,在entryComponents数组中添加组件,如下所示:
export class MyComponent {
constructor(private modal: NgbModal) {}
onClick() {
this.modal.open(CreateAssetModalComponent);
}
}
假设你有其他像NgbModule导入的模块应该可以使用。
答案 1 :(得分:2)
我不确定我完全了解你要做什么,但实质上它非常简单 - 打开一个具体的模态窗口内容您只需在open
服务上调用NgbModal
方法即可。 最简单的可能的实现将是一个单行(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字符串,则不能传递HTML字符串。我想把编译器运送到生产中(你真的不想这样做......)。
所以你的下一个最佳选择是使用另一个组件作为内容,这是一个最小的例子:http://plnkr.co/edit/EJyZ6nF5WVAS9bvQycQe?p=preview
注意:由于Angular本身存在错误,您需要使用open()
打包setTimeout
方法调用。错误参考:https://github.com/angular/angular/issues/15634
答案 2 :(得分:1)
将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()
。