我想显示一个弹出窗口,因为用户单击输入框时工作正常,但我希望该弹窗的数据内容属性来自子组件的模板。这里有一个例子:Angular2 - 使用子组件作为属性的值
parent.ts
import {Component,AfterViewInit} from 'angular2/core';
import {bootstrap} from 'angular2/platform/browser';
import {ChildComponent} from './child_test.ts';
@Component({
selector: 'app',
template: `<input type='text' data-toggle="popover" data-trigger="focus" data-placement="bottom" [attr.data-content]="getPopoverContent()" />`,
providers: [ChildComponent]
})
class AppComponent implements AfterViewInit{
constructor(private _child: ChildComponent) {}
getPopoverContent(){
return this._child; //returning empty object instead of child template
}
ngAfterViewInit(){
$("input").popover();
}
}
bootstrap(AppComponent);
child.ts
import {Component} from 'angular2/core';
@Component({
selector: "child-component",
template: "<div>Popover content from child.</div>"
})
export class ChildComponent{};
我应该使用DynamicComponentLoader,而不是依赖注入?如果是这样,那我该如何实现呢?
为什么你在供应商的组件?它应该在指令上,你应该查询它,而不是像那样注入它。 –