2017-07-27 88 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}”' –