2017-09-18 53 views
0

在html选择器中向组件添加类的正确方法是什么?我有多个应用程序平铺元素,我想为每个元素添加另一个类。我试着简单地将class'tile-one'添加到应用程序切片选择器,但tile.component.scss dosent会看到它。如何使它工作?Angular2将组件传输到组件

tile.component.html:

<app-tile 
    class="tile-one" 
    [title]="'title one'" 
></app-tile> 
<app-tile 
    class="tile-two" 
    [title]="'Title two'" 
></app-tile> 
+0

1)'tile.component.scss'与'tile.component.ts'处于同一级别? 2)是放在'tile.component.scss'内的'tile-one'和'tile-two'类吗? 3)''在'stylesUrls'节的'tile.component.ts'中引用''tile.component.scss'? **在问题**中粘贴来自'tile-component.ts'的代码片段。 – meorfi

回答

0

使用[ngClass]指令。

<div *ngFor="let item of collection"> 
    <label>Name: {{item.name}}</label> 
    <button class="btn" 
      [ngClass]="{'item-updated' : item.isDirty}" 
      [attr.title]="item.isDirty ? 'Item was updated' : null"> 
     Save 
    </button> 
</div> 
+0

我不应该使用ngFor因为每个tile都有另一个[标题]和[内容]。或者我不知道是更好的方式! –

+0

@krolkarpii这只是一个演示。你可以不用'* ngFor'来完成你的场景。重要的片段是'[ngClass]',或者另一种可能性是'[class.yourClass] =“conditional”'。如果你可以用更多的代码更新问题,更好的答案可以解决你的问题。 – meorfi

相关问题