2017-07-27 47 views
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,但我需要它這樣與回調函數。如果你能幫忙,並告訴我,我應該改變而寫的,我將非常感激;)

回答

0

只是通過電流section(當前迭代值)click事件

(click)="switchelement(section)" 

,然後將其分配給activeSectionswitchelement方法。

activeSection: string; 

switchelement (section: string) { 
    this.activeSection = section; 
} 
+0

很好,謝謝:d –

+0

不是真的與此相關的話題,但是,你能告訴我,我怎麼可能會改變第一個div的背景色在這個點擊,每當我點擊其他領域,並重置顏色以前。謝謝 –

+0

@VladimirTomasevicMyNameIsVLT應用基於'activeSesion'的類,你可以使用'ngClass'指令,如'[ngClass] =「{'active':activeSection == section}」' –