angular2模板div点击检查元素是否有类

时间:2016-05-22 18:46:25

标签: angular

我目前有一些逻辑,如果可能的话我只想使用html模板(点击)

我有一个可折叠的div,点击后会变为“活动”

目前我的div是:

<div class="collapsible-header blue darken-2" (click)="getDataForTable($event)">

然后我检查元素

上的类列表
function getDataForTable($event: any){
  let classList = $event.target.classList;

  if(classList.contains("active")){
  //do nothing div is closing
  } else {
  //get data for table since we are opening the div to show the body
  }
}

我希望此(click)操作仅在类不“活动”时触发 (意思是点击“激活”时不要触发);

如何才能使用模板语法?

2 个答案:

答案 0 :(得分:15)

<div #mydiv 
    class="collapsible-header blue darken-2"    
    (click)="mydiv.classList.contains('xxx') ? getDataForTable($event) : null">

答案 1 :(得分:13)

你应该能够这样做:

let graph = CPTXYGraph(frame: self.nativeView.graphView.bounds)

let plotSpace = graph.defaultPlotSpace!
plotSpace.setPlotRange(CPTPlotRange(location: -1, length: 20), forCoordinate: .X)
plotSpace.setPlotRange(CPTPlotRange(location: -10, length: 20), forCoordinate: .Y)
plotSpace.allowsUserInteraction = true
plotSpace.delegate = self

let axisSet = graph.axisSet
if let axis = axisSet?.axisForCoordinate(.Y, atIndex: 0) {
    axis.majorIntervalLength = 5
    axis.minorTicksPerInterval = 5

    let formatter = NSNumberFormatter()
    formatter.positiveFormat = "#"
    formatter.negativeFormat = "#"
    axis.labelFormatter = formatter

    //Here I want to fix this thing..
}

//And then I create a scatter plot and add it and that's it

然后在函数中你只需要添加类:

<div class="collapsible-header blue darken-2" 
     (click)="$event.target.classList.contains('active') || getDataForTable($event)">