结合* ngFor插值以访问全局变量

时间:2019-01-03 04:56:55

标签: angular

使用Angular我创建了以下HTML:

<ng-container *ngFor="let question of questions.questions; let i = index">
  <div *ngIf="i == counter">
    <button *ngFor="let item of question | keyvalue; let buttonIndex = index" class="btn btn-lg btn-primary surveyButton" type="button" value={{item.value}} (tap)="makeSelection(item.value)">
    {{item.value}}

      <span>
        {{prevSelection + buttonIndex}}
      </span>

    </button>
  </div>
</ng-container>

我有全局变量prevSelection1prevSelection2prevSelection3prevSelection4

我希望使用{{prevSelection + buttonIndex}}显示所有这些变量,但是插值内的级联不起作用。

我该如何解决?

1 个答案:

答案 0 :(得分:0)

    You can create object and the try to use it.
    selection = {
       prevSelection1 :'abc',
       prevSelection2 :'def',
       prevSelection3 :'pqr',
       prevSelection4 :'xyz',
    }
<ng-container *ngFor="let question of questions.questions; let i = index">
  <div *ngIf="i == counter">
    <button *ngFor="let item of question | keyvalue; let buttonIndex = index" class="btn btn-lg btn-primary surveyButton" type="button" value={{item.value}} (tap)="makeSelection(item.value)">
    {{item.value}}

      <span>
        {{selection['prevSelection' + buttonIndex]}}
      </span>

    </button>
  </div>
</ng-container>

希望这对您有用