【问题标题】:Displaying icons dynamically in angular 2以角度 2 动态显示图标
【发布时间】:2018-01-03 10:00:12
【问题描述】:

我正在尝试动态加载我的网站的一部分。我在界面中定义了我的图标的所有图标类,如下所示

import { OpaqueToken } from "@angular/core";
import {IAppConfig} from './app.interface'

export let APP_CONFIG = new OpaqueToken("app.config");

export const AppConfig: IAppConfig = {    

employee:"circular tabbaricon users icon",
employee1:"circular tabbaricon users icon"
....etc

};

界面

export interface IAppConfig {

employee:string;
employee1:string;
}

我正在使用

将它导入到我的组件中
constructor( @Inject(APP_CONFIG) private config: IAppConfig) { }

现在我有一个 ngFor,我必须在其中动态显示我的数据。即基于链接名,我必须显示适当的图标。

<div class="ui column" *ngFor="let x of funtionalities.routeLinks ">

<i class={{config+'.'+x.linkName}}  id="iconBack"></i>

</div>

即如何显示图标。我想要类似的东西

 <i class={{config.x.linkName}}></i>

这是否可以做任何方式或有任何其他方式来实现这一点?请帮助

【问题讨论】:

  • 你试过
  • 显示错误无法读取未定义的属性'linkName'

标签: javascript angular typescript angular2-routing


【解决方案1】:

在视图中:

<div class="ui column" *ngFor="let x of funtionalities.routeLinks ">

   <i [ngClass]="getTheClass(x.linkName)"  id="iconBack"></i>

</div>

在课堂上:

import config from "./config.ts'
...
getTheClass(appendix){
    return config[appendix]
}

【讨论】:

  • 不能使用它,因为我必须做类似 class.x 的事情,其中​​ x 将是来自 ngFor 的变量。
  • it dev tools 它显示 ng-reflect-ng-class="config.employee" 这是正确的。但我想要 config.employee 的值,即我想要 {{config.employee}}被分配为类值..不,忽略 iconBack
猜你喜欢
  • 2021-04-29
  • 2021-06-02
  • 2017-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多