我正在尝试使用 ngbModal 来显示用户信息。我打开它两次而不是一次。
info.dialog.html
<template #content let-c="close" let-d="dismiss">
<div class="modal-header">
<button type="button" class="close" aria-label="Close" (click)="d('Cross click')">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title">{{user.firstName}} {{user.lastName}}</h4>
</div>
<div class="modal-body">
<div class="container">
<div class="row">
<div class="col-md-4">
<strong>data1:</strong>
</div>
<div class="col-md-4">
{{user.data1}}
</div>
</div>
<div class="row">
<div class="col-md-4">
<strong>data2:</strong>
</div>
<div class="col-md-4">
{{user.data2}}
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" (click)="c('Close click')">Close</button>
</div>
信息对话框.ts
import { Component, ViewChild, OnInit, OnDestroy, Renderer } from '@angular/core';
import { NgbModal, ModalDismissReasons } from '@ng-bootstrap/ng-bootstrap';
import { User } from '../Classes/user';
import { UserDialogService } from '../Services/user.dialog.service';
@Component({
selector: 'user-info-dialog',
templateUrl: 'app/Dialogs/Templates/user.info.dialog.html'
})
export class UserInfoDialog implements OnInit, OnDestroy {
public user: user;
@ViewChild('content') content: string;
constructor(private dialogService: UserDialogService, private modalService: NgbModal) {
}
ngOnInit() {
this.dialogService.dialogRequest.subscribe( (c: user) => this.show(c) );
}
ngOnDestroy() {
this.dialogService.dialogRequest.unsubscribe();
}
private show(user: user) {
this.user = user;
this.modalService.open(this.content).result.then((result) => {}, (reason) => {});
}
}
user.dialog.service.ts
import { Injectable, OnInit, EventEmitter, Output } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { Http, Headers, Response } from '@angular/http';
import { UsersService } from './users.service';
import 'rxjs/add/operator/toPromise';
import { user } from '../Classes/user';
@Injectable()
export class userDialogService implements OnInit {
@Output() dialogRequest = new EventEmitter<user>();
constructor(private usersService: UsersService) {
}
ngOnInit() {
}
public show(id: number) {
var user = this.usersService.getById(id);
this.dialogRequest.emit(user);
}
}
app.component.ts
<div class="container">
<template ngbModalContainer></template>
<user-info-dialog></user-info-dialog>
<!-- more html code here -->
</div>
所以,基本上,我正在关注何时打开模式的事件。我不能删除app.component.ts中的任何一行,因为它是我的实现所必需的。这就是为什么我得到两个对话框实例的原因。
任何人都可以告诉我如何使用 AngularJS 2 中的模态对话框?将不胜感激一个例子。
谢谢!