1
我有一個簡單的手風琴,我想打開一個字段,關閉所有其他的點擊,併爲每個fealds做。我有一個部分(字段名稱)的數組。我需要的是創建函數,一個用於設置activeSection的值,另一個用於返回activeSection的當前值。簡單的手風琴回調函數
<div *ngFor="let section of sections">
<div class="element-div1" (click)="switchelement()">{{section}}</div>
<div class="element-box-div" *ngIf="activeSection==section"></div>
</div>
和組件:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'element-resources',
templateUrl: './element-resources.component.html',
styles: []
})
export class ElementResourcesComponent implements OnInit {
sections=["Equipment", "People", "Elements", "Tasks", "Locations"];
activeSection = "Equipment";
constructor() { }
ngOnInit() {
}
switchelement (section) {
;
}
}
我知道有一個簡單的版本使用ngIf,但我需要它這樣與回調函數。如果你能幫忙,並告訴我,我應該改變而寫的,我將非常感激;)
很好,謝謝:d –
不是真的與此相關的話題,但是,你能告訴我,我怎麼可能會改變第一個div的背景色在這個點擊,每當我點擊其他領域,並重置顏色以前。謝謝 –
@VladimirTomasevicMyNameIsVLT應用基於'activeSesion'的類,你可以使用'ngClass'指令,如'[ngClass] =「{'active':activeSection == section}」' –