我有一個全局變量public right: any = 30;
如何在angular 4中爲ngfor中的每個元素添加全局變量?
,我想從每個元件唯一地調用這個變量和ngfor 30增加它(循環People
對象):
interface Person {
name: String,
title: String,
content: String,
image: String,
rate: String,
classActive: String,
active: Boolean
}
@Component({
selector: 'app-testimonials',
templateUrl: './testimonials.component.html',
styleUrls: ['./testimonials.component.scss']
})
export class TestimonialsComponent {
people: Person[] = [{
name: 'Douglas Pace',
title: 'Parcelivery Nailed The Efficiency',
content: 'Since I installed this app, its always help me book every tickets I need like flight, concert, ' +
'movie or hotel. I don\'t need to install different app for different ticket. The payment is also very easy',
image: '../../assets/img/profile_pics/profile_pic.jpg',
rate: '4.5',
classActive: 'testimonials__selected-visible',
active: true
},
{
name: 'Naseebullah Ahmadi',
title: 'Parcelivery Nailed The Efficiency',
content: 'Since I installed this app, its always help me book every tickets I need like flight, concert, ' +
'movie or hotel. I don\'t need to install different app for different ticket. The payment is also very easy',
image: '../../assets/img/profile_pics/profile_pic.jpg',
rate: '4.5',
classActive: '',
active: false
},
{
name: 'Haseebullah Ahmadi',
title: 'Parcelivery Nailed The Efficiency',
content: 'Since I installed this app, its always help me book every tickets I need like flight, concert, ' +
'movie or hotel. I don\'t need to install different app for different ticket. The payment is also very easy',
image: '../../assets/img/profile_pics/profile_pic.jpg',
rate: '4.5',
classActive: '',
active: false
}
];
public right: any = 30;
constructor() {}
stackItem(a) {
console.log(a);
}
}
<div *ngFor="let person of people; let last = last"
class="testimonials__card-container"
#__person
[ngClass]="{'testimonials__card-container--not-visible': !person.active}"
[style.right]="stackItem(__person.right)">
</div>
我記得在Angular 2中,我們可以將全局變量作爲ngfor中的每個元素的實例。但是在angular4中情況並非如此。有沒有其他方法?
1.我對你試圖達到的目標有點困惑。我猜console.log(a);給'indefined ...'? – Vega
我想要的是通過傳遞一個全局變量的實例來爲我們的人物對象中的每個元素增加我的全局變量。 是的,console.log()打印未定義,即使我將全局變量賦值爲30. @Vega – James
__person.right不是全局變量。 right是一個全局變量,#_person是一個模板變量 – Vega