- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试构建一个多选组件,它可以为每个选择选项元素提供一个模板。它大部分都在工作,模板是从选项组件中的 ng-content 中获取的,并显示在“选择弹出框”中。
尝试在选择组件中显示所选选项时遇到问题。好像模板一次只能显示在一个地方,所以当select的选项显示在select中时,就从popover中移除了。
有什么方法可以克隆 TemplateRef 组件吗?我实际上并不介意模板的上下文是否更新,因为我目前没有使用其中的任何内容,只是模板。
为了清楚起见,包括下面的一些代码片段,我认为没关系。
somepage.component.html
中的用法:
<my-select [(value)]="vals">
<my-option *ngFor="let x of [1,2,3]" [value]="x">Option {{x}}</my-option>
</my-select>`
...
<ng-container [ngTemplateOutlet]="getSelectedOptionTemplate()"></ng-container>
...
getSelectedOptionTemplate(){
// Some way to clone here could solve the issue?
return this.getSelectedOption().template;
}
...
<ng-container *ngFor="let opt of options;">
<ng-container [ngTemplateOutlet]="getTemplate(opt)"></ng-container>
</ng-container>
...
<ng-template>
<ng-content></ng-content>
</ng-template>
...
@ViewChild(TemplateRef)
template: TemplateRef<any>;
...
最佳答案
我在 Github 上找到了这个问题的解决方案。
似乎使用相同的TemplateRef
当 ng-template
时工作正在包装组件 包含 ng-content
.
想象一下,我们需要获得类似的东西:
<ng-template>
<my-option>
... option content ...(<ng-content></ng-content>)
</my-option>
</ng-template>`
为了得到上面的结构,我们可以实现一个
structural directive .从文档:
Angular transforms the asterisk in front of a structural directiveinto an
<ng-template>
that surrounds the host element and itsdescendants.
@Directive({
selector: "[appOptionTemplate]"
})
export class OptionTemplateDirective {
@Input("appOptionTemplate")
value: any;
constructor(
public templateRef: TemplateRef<any>
) {}
}
(注意以下几点:
templateRef
在构造函数中Input
属性(property)) <app-select>
<ng-container *ngFor="let x of [1,2,3]; let i=index;">
<app-option *appOptionTemplate="x">
Option {{x}}
</app-option>
</ng-container>
</app-select>
现在,我们将使用结构指令来查询
select
中的内容。零件:
@ContentChildren(OptionTemplateDirective)
options: QueryList<OptionTemplateDirective>;
为了显示该选项,我们将使用:
<ng-container *ngTemplateOutlet="option.templateRef"></ng-container>
示例:
关于Angular 一次在多个地方使用相同的 TemplateRef - 克隆 TemplateRef?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53064929/
我正在尝试构建一个多选组件,它可以为每个选择选项元素提供一个模板。它大部分都在工作,模板是从选项组件中的 ng-content 中获取的,并显示在“选择弹出框”中。 尝试在选择组件中显示所选选项时遇到
我正在尝试创建一个根据条件创建 View 的指令。就像内置的 ngIf 指令一样。 这是我添加到项目中的所有内容: export class QueryNameContext { public
如果答案是已接受的答案,我会尝试显示复选标记: template: `✔` 但是我得到这个错误: EXCEPTION: No provider for TemplateRef! (NgI
也许我从根本上误解了某些东西,但我试图让 TemplateRef 指向我的组件的模板(在下面的简单示例中只是“Hello”)。我已将我的代码精简到最低限度以尝试找出问题所在: import { Com
也许我从根本上误解了某些东西,但我试图让 TemplateRef 指向我的组件的模板(在下面的简单示例中只是“Hello”)。我已将我的代码精简到最低限度以尝试找出问题所在: import { Com
我希望将组件的 ng-content 的内部文本作为简单字符串获取,以便能够重用它。 我尝试过这样的事情: @Component({ selector: 'my-comp', template
我必须将所有 ngx-bootstrap modals 放在一个 component.html 中,这样我就可以从任何地方访问任何模态。 我试过下面的代码 header.componentn.html
最近我开始积极使用模板来自定义我的 UI 组件。我还使用 OnPush 变化检测策略来优化性能。如果我将模板传递给嵌套组件,一切都会顺利进行。用户操作触发嵌套组件中的更改检测,它向上移动到定义模板的原
我在创建基于自定义 Observable 和自定义项目 TemplateRef 呈现项目集合的组件时遇到问题。 该组件将 Observable 和 TemplateRef 作为输入并适本地呈现它们。问
有没有办法为服务中的现有 html 模板获取 TemplateRef? 问题的背景(因为我一开始可能做错了什么): 我正在创建一个使用 NgbModal 的服务在弹出窗口中显示一些文本。模态需要一个
我们正在构建一个 angular 4 组件库,其中一个组件是 Busy 组件。该组件的目的是允许开发人员在任何包含微调器图形的给定 HTML 元素上创建叠加层。 ... 当值isBusy是真的我们想追
我对如何通过传递 string 来获取 TemplateRef 很感兴趣。 我不想通过组件 html 获取它,因为我只想将它保存在配置中以供所有组件使用。我指的是可以在此处查看的 ng-bootstr
组件 HTML: 组件 TS: @ViewChild('content') public content: TemplateRef; Visual Studio 消息: [ts] Generic
最近我有一个任务,我需要将一个 TemplateRef 传递给组件,它提供一些服务,比如 SomeService。在该模板中,我使用了一个注入(inject)服务 SomeService 的指令。我最
我试图改进我的代码而不是有条件,所以我决定创建一个指令或者如果可能的话创建一个管道,它可以帮助我根据其类型(字符串或模板引用)打印选项卡的标题,我的代码如下,此代码用于我的 Tabs/Tab 组件
我得到的组件 假设我有以下 super 简单的组件: class MyComponent { @Input() simpleContent: string; @ContentC
让我们假设我有显示数据列表的泛型类型的组件: interface DataProvider { getData(): T[]; } ... template: ' ' ...
在 Angular 中试验 ElementRef、TemplateRef、ViewRef 和 ViewContainerRef。我已经创建了一个 HTML 模板,我想从那里创建一个 View 并将该
我正在尝试获取 ng-template 和 input 参数的 TemplateRef。 应用程序组件.html: {{dataItem|json}} 问题是我
我想使用 ViewChildren 从 TemplateRef 生成 QueryList,但无法传递到输入组件。 例如 组件.ts: @ViewChildren(TemplateRef) cellTe
我是一名优秀的程序员,十分优秀!