Angular 2中的条件调用函数

时间:2016-11-04 05:03:06

标签: javascript html angular

我有这样的模板

 <div class="action" *ngFor="let action of (actions | async)">
          <div
            class="collapsible action-table-row"
            [attr.aria-expanded]="actionCollapse.isActive()"
            [class.collapse-table-row]="actionCollapse.isActive()"
            (click)="openActionCollapse(actionCollapse)"

          >

如果条件满足,我想调用一个函数,类似这样的

 <div class="action" *ngFor="let action of (actions | async)">
          <div
            class="collapsible action-table-row"
            [attr.aria-expanded]="actionCollapse.isActive()"
            [class.collapse-table-row]="actionCollapse.isActive()"
            (click)="openActionCollapse(actionCollapse)"
            (action.value=="")="callfunction()" //<---- here
          >

我怎样才能做到这一点。

1 个答案:

答案 0 :(得分:0)

<div class="action" *ngFor="let action of (actions | async)">
          <div
            class="collapsible action-table-row"
            [attr.aria-expanded]="actionCollapse.isActive()"
            [class.collapse-table-row]="actionCollapse.isActive()"
            (click)="openActionCollapse(actionCollapse)">

         {{ action.value=="something" ? callfunction() : '' }} //<---- here

</div>

推荐方式

<div class="action" *ngFor="let action of (actions | async)">
          <div
            class="collapsible action-table-row"
            [attr.aria-expanded]="actionCollapse.isActive()"
            [class.collapse-table-row]="actionCollapse.isActive()"
            (click)="openActionCollapse(actionCollapse)">

        {{ action.value=="something" ? someVar : '' }} //<---- here

</div>

constructor(){
     this.someVar=this.callfunction();
}

callfunction()
{
   return "something";
}