angular - 如何使用属性设置 Angular 4 组件的样式?

标签 angular typescript attributes

我有一个组件,我想将某些数据传递给 ngFor 中的子组件通过属性。基于属性,我想最终设置这些子组件的样式。

代码

<强> testimonials.component.html - (Parent component)

<ng-template ngFor let-person="$implicit" [ngForOf]="peopleAsPreviousCurrentAndNextTriplets" let-i=index let-last=last>
    <app-card
           [prevCard]      ="last ? person.previous : null"
           [currentCard]   ="last ? person.current : null"
           [nextCard]      ="last ? person.next : null"
    ></app-card>
</ng-template>

<强> card.component.ts - (Child component)

import {Component, Input, ViewChild} from '@angular/core';
import {Person} from '../testimonials/person';

@Component({
  selector: 'app-card',
  templateUrl: './card.component.html',
  styleUrls: ['./card.component.scss']
})
export class CardComponent {
  @Input() decrement:     Number;
  @Input() defaultDiff:   Number;
  @Input() currentCard:   Person;
  @Input() prevCard:      Person;
  @Input() nextCard:      Person;
  @ViewChild('card')      card;

  constructor() { }
}

我想要样式 <app-card>[currentCard]属性。

<强> child.component.sass

:host {
  &[currentCard] {
    .testimonials__card-container {
      background-color: black;
    }
  }
}

不确定为什么上面的样式没有被应用。

请帮忙。

谢谢

编辑

这是可视化我的固定卡片:

enter image description here

最佳答案

不要混淆 html 属性和 Angular 绑定(bind)。可行的方法是这样的:

在父 View 中:

   <app-card [class.currentcard]= "last"
               [prevCard]      ="last ? person.previous : null"
               [currentCard]   ="last ? person.current : null"
               [nextCard]      ="last ? person.next : null">
    </app-card>

并在子级的样式表中:

:host(.currentcard) {
   background-color: black;
}

关于angular - 如何使用属性设置 Angular 4 组件的样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45697181/

相关文章:

html - 如何更改滚动 ionic 内容的颜色?

c# - 有人得到了 C# MVC 的出生日期验证属性吗?

asp.net-mvc-3 - Ninject 绑定(bind)属性以使用构造函数参数进行过滤

使用 FCM 的 Angular 推送通知

html - display flex 不会像预期的那样打断长行

javascript - 如何设置ag网格中默认选择的第一行(Angular)

php - CakePHP、jQuery - 将数据属性添加到 anchor

angular - 无法分配给对象的只读属性 'stopImmediatePropagation'

带 FormControl 的 Angular Mat 选择触发器

visual-studio - TypeScript 1.6 Ionic 错误 TS2322 Build Type is not assignable to type 'IActionSheetOptions'