[英]Angular2 component - render html from the index page
Is it possible to render html which is inside app-nav
tag already, rather then providing it in templateUrl
? 是否可以渲染
app-nav
标签内的html,而不是在templateUrl
提供?
@Component({
selector: 'app-nav',
// commented out - templateUrl: './nav.component.html',
styleUrls: ['./nav.component.scss']
})
export class NavComponent {
title: string = 'This is my title';
}
Html that is already on the html page. 已经在html页面上的Html。
<app-nav>
nav works! {{ title }}
</app-nav>
If I uncomment the templateUrl
then app-nav will be replaced by nav-component.html page, but I dont want that. 如果我取消注释
templateUrl
那么app-nav将被nav-component.html页面替换,但我不希望这样。 I have dynamic html and I want to render that. 我有动态HTML,我想渲染它。
You can use embedded view with ngTemplateOutlet
projection. 您可以将嵌入视图与
ngTemplateOutlet
投影一起使用。 Wrap your content within <app-nav>
tags in <template>
. 在
<template>
<app-nav>
标记内包装您的内容。 Than in your NavComponent
find this TemplateRef
with ContentChild
and insert this templateRef into component's template passing context that contains your title variable to it. 在您的
NavComponent
使用ContentChild
找到此TemplateRef
,并将此templateRef插入到组件的模板中,并将包含您的标题变量的上下文传递给它。 Something like this: 像这样的东西:
@Component({
selector: 'app-nav',
templateUrl: './nav.component.html',
styleUrls: ['./nav.component.scss']
})
export class NavComponent {
title: string = 'This is my title';
@ContentChild('defaultTemplate') defaultTemplate = null // get templateRef
}
In nav.component.html create template outlet with relative template context 在nav.component.html中,使用相对模板上下文创建模板插座
<template [ngOutletContext]="{ title: title }" [ngTemplateOutlet]="defaultTemplate"></template>
....other component content....
And then in place of component use: 然后代替组件使用:
<app-nav>
<template #defaultTemplate let-title="title">
nav works! {{ title }}
</template>
</app-nav>
UPD: UPD:
Here is a plunk with example 这是一个有例子的插件
in app/some.component.ts
there is a ngTemplateOutlet
projection from app/app.component.ts
template
在
app/some.component.ts
, app/app.component.ts
template
有一个ngTemplateOutlet
投影
UPD: UPD:
Ok, there is a way to get initial content from index.html into the component. 好的,有一种方法可以将index.html的初始内容放入组件中。 You can use
APP_INITIALIZER
function that will be executed when an application is initialized. 您可以使用将在初始化应用程序时执行的
APP_INITIALIZER
函数。
app/root-template.initializer.ts
app/root-template.initializer.ts
In app/app.component.ts
I just replace relevant property with initial content. 在
app/app.component.ts
我只是用初始内容替换相关属性。 This is a bit hacky way and should be done by replacing template in ComponentMetadata
which is obtained with Reflect.getMetadata: 这有点hacky方式,应该通过替换使用Reflect.getMetadata获得的
ComponentMetadata
中的模板来完成:
const annotations = Reflect.getMetadata('annotations', NavComponent) const meta = annotations.find(annotation => annotation instanceof ComponentMetadata) meta.template = meta.template.replace( '{{ someVarInTemplate }}', 'initialContentInIndex' )
This way the component template will have initial index content and it will be parsed by angular. 这样,组件模板将具有初始索引内容,并且将通过angular解析。 More about Reflect here
更多关于在这里反思
@Yaroslav, expanding on your solution: @Yaroslav,扩展您的解决方案:
Looks like a transclusion (in Angular 1 terms), so in Angular 2 you can use ng-content on the inner component. 看起来像是一个翻译(在Angular 1术语中),所以在Angular 2中你可以在内部组件上使用ng-content。
<div class="my-component"> <ng-content></ng-content> </div>
To get interpolation working on outer transcluded markup, give the element an id and prefix the interpolated content with it. 要在外部转换标记上进行插值处理,请为元素指定一个id,并为其插入插值内容的前缀。
<my-component #comp> This is my transcluded content! ++{{comp.title}}++ </my-component>
Don't try to transclude from index.html , it's not an angular component, so it doesn't seem to work as the outer component. 不要试图从index.html转换 ,它不是一个角度组件,所以它似乎不作为外部组件。 If you use app.component as the outer and another my.component as inner, it works.
如果你使用app.component作为外部,另一个my.component作为内部,它可以工作。
Here's a fork of your plunkr with the changes. 这是你的修补者的一个分叉。 plnkr
plnkr
For reference, I used Todd Motto's excellent article on angular 2 transclusion: ref here . 作为参考,我使用了Todd Motto关于角度2转换的优秀文章: ref here here 。
The angular guide only vaguely refers to ng-content (with a link that 404's), so I wonder if it's disappearing. 角度指南只是模糊地指的是ng-content(带有404的链接),所以我想知道它是否正在消失。 May be superseded by ngComponentOutlet ref here
这里可能会被ngComponentOutlet ref取代
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.