yoo*_*uri 1 ngx-bootstrap angular
我有两个组件,客户和客户的组件。客户组件包含一个客户列表,当我点击一个客户时,客户组件应该打开一个模式。
客户.component.ts
import { Component, OnInit, } from '@angular/core';
import { CustomerComponent } from '../customer/customer.component';
export class Customer {
id: number;
name: String;
}
const CUSTOMERS: Customer[] = [
{
id: 1,
name: 'Customer one'
},
{
id: 2,
name: 'Customer two'
}
];
@Component({
selector: 'app-customers',
templateUrl: './customers.component.html',
styleUrls: ['./customers.component.css'],
providers: [
CustomerComponent
]
})
export class CustomersComponent implements OnInit {
title: String;
customers: Customer[] = CUSTOMERS;
customerComponent: CustomerComponent;
// or
@ContentChild(CustomerComponent)
public customerComponent: CustomerComponent;
constructor() {
this.title = 'Customers';
this.customerComponent = new CustomerComponent();
// tried removing
}
ngOnInit() {
}
showCustomer(customer: Customer) {
this.customerComponent.openModal(customer);
}
}
Run Code Online (Sandbox Code Playgroud)
客户.component.html
<app-customer></app-customer>
<div id="page-wrapper">
<div class="container-fluid">
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">{{title}}</h1>
</div>
</div>
<div class="row">
<div class="col-lg-12">
<div class="panel panel-default">
<div class="panel-heading">
De klanten pagina
</div>
<div class="panel-body">
<div class="table-responsive">
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th>#</th>
<th>Name</th>
</tr>
</thead>
<tbody *ngFor="let customer of customers;">
<tr (click)="showCustomer(customer)" [style.cursor]="'pointer'">
<td>{{customer.id}}</td>
<td>{{customer.name}}</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
所以在点击 a 之后tr,CustomersComponent 应该显示一个模式。
客户.component.ts
import { Component, OnInit, ViewChild } from '@angular/core';
import { Customer } from '../customers/customers.component';
import { ModalDirective } from 'ngx-bootstrap';
@Component({
selector: 'app-customer',
templateUrl: './customer.component.html',
styleUrls: ['./customer.component.css'],
})
export class CustomerComponent implements OnInit {
@ViewChild('lgModal') public lgModal: ModalDirective;
// or
@ViewChild(ModalDirective) public lgModal: ModalDirective;
constructor() {
}
ngOnInit() {
}
openModal(customer: Customer): void {
console.log(customer);
console.log(this.lgModal);
this.lgModal.show();
}
}
Run Code Online (Sandbox Code Playgroud)
和customer.component.html
<div bsModal #lgModal="bs-modal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title pull-left">Large modal</h4>
<button type="button" class="close pull-right" (click)="lgModal.hide()" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
...
</div>
</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
console.log(this.lgModal);返回undefined并this.lgModal.show();给出ERROR TypeError: Cannot read property 'show' of undefined错误..
编辑:
试图添加<app-customer></app-customer>到customers.component.html
由于这个问题,我为您的问题找到了解决方案:https : //www.bountysource.com/issues/35264857-how-to-call-modal-from-parent-component
所做的更改是让孩子成为exportAs: 'child'(例如)
@Component({
....
exportAs: 'child'
})
Run Code Online (Sandbox Code Playgroud)
然后在父模板中有:
<app-customer #c="child"></app-customer>
Run Code Online (Sandbox Code Playgroud)
在父模板中,如果您愿意,您可以直接在子模板中调用您的方法:
(click)="c.openModal(customer)"
Run Code Online (Sandbox Code Playgroud)
在孩子中,您声明您的@ViewChild:
@ViewChild('lgModal') public lgModal: ModalDirective;
Run Code Online (Sandbox Code Playgroud)
应该这样做!
这是一个可以玩的DEMO :)
| 归档时间: |
|
| 查看次数: |
20060 次 |
| 最近记录: |