2017-08-15 91 views
2

我有一個組件,我想通過屬性將某些數據傳遞給ngFor中的子組件。基於屬性,我想最終設計這些子組件。如何設置具有屬性的角度4組件?

代碼

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

+1

'[currentCard]'是*綁定*,它實際上並不適用該屬性。嘗試在條件上將元素放在元素上。 – jonrsharpe

+0

你的問題是我每天服用的SO。這次真是萬分感謝。 – Moshe

+0

大聲笑,林不知道應該爲此感到高興。道歉,如果我給你造成任何困擾@Moshe – James

回答

4

不要混淆HTML屬性和角度綁定。什麼工作是這樣的:在父視圖

<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; 
} 
+0

OMG,我真的很抱歉沒有儘快回覆你。是的,這是非常有用的,我真的很感激。只是一個問題,但。我來自Polymerjs背景,我們的做法是根據您傳遞的屬性來設計風格。然後將其定位到主機中。下面我問了一個類似的問題。但它最終沒有奏效。我misundertood它︰https://stackoverflow.com/questions/45691491/how-to-style-angular4-custom-components-based-on-attribute/45692412#45692412 – James

+0

雖然它可以做到angular4嗎?我首先不希望增加班級。只需在主機內部設置它的樣式 – James

+0

不完全是這樣,所以將其視爲一個畫廊(堆疊物品加註器) - 當前一個將位於堆疊頂部,[prevCard]將位於當前卡的後面,但是左側,[右側卡]將位於當前卡的後面,但位於右側。並且會有兩個按鈕,當我點擊下一個時,當前卡變成prevCard,下一個卡變成當前卡。你明白了嗎? – James