如何使用DynamicComponentLoader在单击时实例化子组件?
我正在使用angular2.0.0-beta.15。基本上,我试图在点击标题中的按钮时加载一个模式窗口。我已经把模态的模板放到了html中,我正在尝试的是,在父组件中单击按钮时,一个指示按下哪个按钮的参数被传递给子组件。基于此,我从数据库中获取模态数据,并将其放入模态窗口。下面是一段代码 parent.html如何使用DynamicComponentLoader在单击时实例化子组件?,dynamic,angular,modal-dialog,components,loader,Dynamic,Angular,Modal Dialog,Components,Loader,我正在使用angular2.0.0-beta.15。基本上,我试图在点击标题中的按钮时加载一个模式窗口。我已经把模态的模板放到了html中,我正在尝试的是,在父组件中单击按钮时,一个指示按下哪个按钮的参数被传递给子组件。基于此,我从数据库中获取模态数据,并将其放入模态窗口。下面是一段代码 parent.html <modal-child id="ContactModal"></modal-child> <-- code goes here--> <
<modal-child id="ContactModal"></modal-child>
<-- code goes here-->
<li [style.display]="!showContact?'none':'inherit'"><a (click)="getContact()" data-remote="false"
data-toggle="modal" data-target="#ContactModal"><i class="fa fa-question"></i>Contact</a></li>
<-- code goes here-->
<!-- Contact -->
<div id="ContactModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content -->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Contact</h4>
</div>
<div class="modal-body">
<div [innerHTML]="contactData"></div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
模态子组件
import {Component, Input} from 'angular2/core';
import {UserService} from '../services/data-services.service';
@Component({
selector: 'modal-child',
templateUrl: './views/templates/modalTemplate.html'
})
export class ModalComponent {
@Input('id') modalId: string;
private contactData: string;
constructor(private _dataService: DataService) {
console.log('id:' + this.modalId);
if (this.modalId === 'Contact') {
this.getContactData();
}
}
getContactData() {
this._dataService.getContact().subscribe(data => {
console.log('contact:' + data);
this.contactData = data;
});
}
}
modalTemplate.html
<modal-child id="ContactModal"></modal-child>
<-- code goes here-->
<li [style.display]="!showContact?'none':'inherit'"><a (click)="getContact()" data-remote="false"
data-toggle="modal" data-target="#ContactModal"><i class="fa fa-question"></i>Contact</a></li>
<-- code goes here-->
<!-- Contact -->
<div id="ContactModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content -->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Contact</h4>
</div>
<div class="modal-body">
<div [innerHTML]="contactData"></div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
&时代;
接触
接近
当我尝试上面的代码时,它在组件“ParentComponent”的视图中给出了类似“意外指令值”[object object]”的异常。有人能指导我吗?这是我自己解决的。您好,请问您是如何解决这个问题的?是的。你是如何解决的?对不起,我无法帮助你,因为我无法回忆起上面的情景以及我是如何解决的。这是我自己解决的。你好,你是如何解决这个问题的?是的。您是如何解决的?对不起,我无法帮助您,因为我无法准确地回忆上面的场景以及我是如何解决的。