[英]Angular2 - Get component html before template
I have 2 components A and B defined this way: 我有两个组件A和B这样定义:
Component A: 组件A:
import { Component, CORE_DIRECTIVES, ElementRef} from 'angular2/angular2';
import ComponentB from ".'ComponentB";
@Component( {
selector: 'component-a',
directives: [ CORE_DIRECTIVES ],
template: `
<component-b>
Inner text/html, whatever...
</component-b>
`
} )
export class ComponentA( {
static parameters = [ [ ElementRef ] ];
element:ElementRef;
constructor( element:ElementRef ) {
this.element = element;
}
afterViewInit():void {
}
} )
Component B: 组件B:
import { Component, CORE_DIRECTIVES, ElementRef} from 'angular2/angular2';
@Component( {
selector: 'component-b',
directives: [ CORE_DIRECTIVES ],
template: `
<h1>{{innerContent}}</h1>
`
} )
export class ComponentB( {
static parameters = [ [ ElementRef ] ];
element:ElementRef;
innerContent:string;
constructor( element:ElementRef ) {
this.element = element;
}
afterViewInit():void {
}
} )
What i want to accomplish is to use the content between the <component-b>
tag in Component A template to set the variable innerContent
of ComponentB. 我想要完成的是使用组件A模板中<component-b>
标记之间的内容来设置ComponentB的变量innerContent
。
I have tried to use this.element.nativeElement.innerHTML
inside afterViewInit
of ComponentB class, but it gets the innerHTML of the template variable, not of the parent one. 我试图在ComponentB类的afterViewInit
中使用this.element.nativeElement.innerHTML
,但它获取模板变量的innerHTML,而不是父模板变量。
Restrictions: Use of custom attributes in the component using @Inputs/@Outputs is not an option. 限制:不能使用@ Inputs / @ Outputs在组件中使用自定义属性。 eg 例如
<component-b [innerContent]="Inner text/html, whatever...">
</component-b>
In ComponentB template, put this: 在ComponentB模板中,输入:
template: `
<h1><ng-content></ng-content></h1>
`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.