【发布时间】:2016-04-23 16:22:50
【问题描述】:
如果调用 show(),我计划向 DOM 添加一个动态组件。 我知道有一个使用 ngIf 或 [hidden] 的解决方案可以隐藏它并将其用作指令,但我不喜欢这个解决方案,因为我不想在我的 HTML 中声明它。
import {Component} from 'angular2/core';
import {InfoData} from '../../model/InfoData';
@Component({
selector: 'Info',
templateUrl: './components/pipes&parts/info.html',
styleUrls: ['./components/pipes&parts/info.css']
})
export class Info{
infoData: InfoData;
public show(infoData: InfoData) {
this.infoData= infoData;
document.body.appendChild(elemDiv); <----- Here?
}
}
然后我将其声明为 Provider,以便我可以调用 show()。
import {Component} from 'angular2/core';
import {Info} from './components/pipes&parts/Info';
@Component({
selector: 'Admin',
templateUrl: './Admin.html',
styleUrls: ['./Admin.css'],
directives: [Info],
providers: [Info]
})
export class Admin {
constructor(private info: Info) {
info.show(); <---- append the Info Element to DOM
}
【问题讨论】:
标签: javascript components angular