繁体   English   中英

Angular2。 如何在标签“ href”中使用* ngFor设置“ URL”,并在条件*​​ ngIf中设置“ {{theme.name}}”?

[英]Angular2. How can I use *ngFor for set “URL” in the tag “href” and set '{{theme.name}}' in the condition *ngIf?

我需要切换模板。 我有12个CSS文件需要动态加载

我有一个模板html。 例如,它的工作原理(硬代码):

<div class="container">
    <link rel='stylesheet'   href="/app/switchTemplate/css/amelia/bootstrap.min.css" *ngIf="currentTheme.name === 'amelia'" />
</div>

当我对标签“ href”使用<* ngFor =“ let theme of themes”>时,它不起作用(动态)

<div class="container">
    <link rel='stylesheet'  *ngFor="let theme of themes"  href="{{theme.url}}" *ngIf="currentTheme.name === '{{theme.name}}'" />
</div>

如何在标签“ href”中使用* ngFor设置“ URL”,并在条件*​​ ngIf中设置“ {{theme.name}}”?

单个元素上不能有多个结构指令。 您可以使用<template>标记,但是新的<ng-container>元素更为方便,因为它的语法与普通元素上的结构指令的语法相同:

<div class="container">
  <ng-container *ngFor="let theme of themes">
    <link rel='stylesheet'    href="{{theme.url}}" *ngIf="currentTheme.name === theme.name" />
  </ng-container>
</div>

<template><ng-container>元素仅由Angular2内部处理,并且从未添加到DOM中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM