在特定按钮上,单击“添加离子3”

时间:2019-03-21 10:58:57

标签: angular ionic-framework ionic3

我有一个情况,我有多个记录,并且我在那个*ngFor循环中使用过,所以每条记录都有一个按钮,而我想做的是onclick更改按钮的背景,以便它看起来很像被点击

<div class="main" *ngFor="let data of fetched_info; let i=index">
    <ion-grid no-padding no-margin>
        <ion-row no-padding no-margin class="row">
            <ion-col col-4 no-padding no-margin> 
                <button ion-button small icon-start color="secondary"
outline (click)="shortlist($event,data[i].username)" class="">
                    <ion-icon ios="ios-star-outline" md="md-star-outline"></ion-icon>
                    Shortlist
                </button>
            </ion-col>

        </ion-row>
    </ion-grid>
</div>
我要实现的onclick功能的

shortlist()

1 个答案:

答案 0 :(得分:1)

您可以尝试

HTML

<div class="main" *ngFor="let data of fetched_info; let i=index">
 <ion-grid no-padding no-margin>
     <ion-row no-padding no-margin [ngClass]="backgroundColorFlag[i] == true ? 'backgroundColorClass' : 'backgroundColorNormalClass' " class="row">
       <ion-col col-4 no-padding no-margin> 
         <button ion-button small icon-start color="secondary"
           outline (click)="shortlist($event,data[i].username, i)" class="">
           <ion-icon ios="ios-star-outline" md="md-star-outline"></ion-icon>
            Shortlist
         </button>
       </ion-col>
     </ion-row>
 </ion-grid>
</div>

TS

    backgroundColorFlag: any[] = [];
    shortlist(data, i) {
      this.backgroundColorFlag[i] != backgroundColorFlag;
    }
    functionWhichHavefetched_info() {
     for (let i = 0; i < fetched_info.length; i++) {
           this.backgroundColorFlag[i] = false // default we are setting value false and on click we set this flag value true
      }
    }

您需要创建一个类似于 backgroundColorClass backgroundColorNormalClass 的类,并使用标志 backgroundColorFlag 添加或删除该类,希望对您有所帮助