离子幻灯片上每个按钮的单击事件

时间:2018-10-03 09:17:23

标签: typescript ionic-framework ionic2

我有各种幻灯片,每个幻灯片都有“喜欢”和“评论”按钮,当我单击特定的幻灯片(如按钮)时,该按钮图像发生变化或显示为您和20个喜欢,我必须做的是。

我面临的问题是,当我单击“喜欢”按钮时,它也会影响其他幻灯片,例如“同样”按钮。当我刷新页面时,即使我已经喜欢它,它仍然显示为特定幻灯片启用了按钮。

我的代码在

下方

.html

<ion-slides #slider (ionDidChange)="SlideChanged()" [options]="_sliderOptions" id="ionSlider" class="main-slider">
    <ion-slide #slide *ngFor="let robot of _robots; let i = index">
      <ion-grid class="content-info-item">
          <img [src]="sanitizer.bypassSecurityTrustResourceUrl(photoPicArray[i])" class="postimage" />
        <div class="content-info-content">
          <p class="posttime">{{robot.Created | date:'medium'}}</p>
          <h1 class="GuardianEgyp-Regular-Web heading-title" (click)="goToLongStory(i)">{{robot.Title}}</h1>
          <p class="shortstory GuardianEgyp-Light-Web" (click)="goToLongStory(i)">{{robot.SortStory}}</p>
        </div>
        <div class="like-info-icon" *ngIf="ionicNamedColor == false">
            <button (click)="toggleNamedColor(robot.Id,i)" color="primary" block>
                <img src="assets/imgs/like.png">
           </button>
        </div>
        <div class="like-info-content GuardianEgyp-Light-Web">
          <p item-end><span *ngIf="ionicNamedColor == true" >You and</span><span>{{robot.TotalLikesId}}</span> Likes</p>
      </div>

      <div class="like-info-icon-box">
          <div class="like-info-icon">
            <img class="" width="24" src="assets/imgs/comments1.png" alt="longstory" title="View More">
          </div>
          <div class="like-info-content GuardianEgyp-Light-Web">
            <p>
              <span>{{robot.TotlaCommentsId}}</span> Comments</p>
          </div>
        </div>
      </ion-grid>

.ts文件

public ionicNamedColor: boolean = false;
  public likeArray: any[] = [];


    public toggleNamedColor(articleID: string, indexID: string): void {
        if (this.ionicNamedColor == false) {
          this.ionicNamedColor = true;
        }
        else {
          this.ionicNamedColor = false;
        }
}

SlideChanged() {
    let currentSlide = this.slider.getActiveIndex();
    let slideElements = this.slides.toArray();

    let targetSlide = slideElements[currentSlide];
    console.log(targetSlide);
  }

3 个答案:

答案 0 :(得分:1)

您对数组的所有项目使用相同的变量ionicNamedColor。

您可以尝试像在机器人对象ionicNamedColor中创建另一个变量,然后更改特定索引对象颜色的颜色。

Ex:* ngIf =“ robot.ionicNamedColor == false”

答案 1 :(得分:1)

您可以实现从.ts向_robots JSON对象添加新密钥,这是提示:

在您的.html文件中

<ion-slides #slider (ionDidChange)="SlideChanged()" [options]="_sliderOptions" id="ionSlider" class="main-slider">
    <ion-slide #slide *ngFor="let robot of _robots; let i = index">
      <ion-grid class="content-info-item">
          <img [src]="sanitizer.bypassSecurityTrustResourceUrl(photoPicArray[i])" class="postimage" />
        <div class="content-info-content">
          <p class="posttime">{{robot.Created | date:'medium'}}</p>
          <h1 class="GuardianEgyp-Regular-Web heading-title" (click)="goToLongStory(i)">{{robot.Title}}</h1>
          <p class="shortstory GuardianEgyp-Light-Web" (click)="goToLongStory(i)">{{robot.SortStory}}</p>
        </div>
        <div class="like-info-icon" *ngIf="robot.isLiked == false">
            <button (click)="toggleNamedColor(robot.Id, i)" color="primary" block>
                <img src="assets/imgs/like.png">
           </button>
        </div>
        <div class="like-info-content GuardianEgyp-Light-Web">
          <p item-end><span *ngIf="robot.isLiked == true" >You and</span><span>{{robot.TotalLikesId}}</span> Likes</p>
      </div>

      <div class="like-info-icon-box">
          <div class="like-info-icon">
            <img class="" width="24" src="assets/imgs/comments1.png" alt="longstory" title="View More">
          </div>
          <div class="like-info-content GuardianEgyp-Light-Web">
            <p>
              <span>{{robot.TotlaCommentsId}}</span> Comments</p>
          </div>
        </div>
      </ion-grid>

在您的.ts文件中

let _robots = [
  {
    Id : "1",
    Title: "your title...",
    SortStory: "SortStory...",
    ...
  },{
    Id : "2",
    Title: "your title...",
    SortStory: "SortStory...",
  }
]

function addKeyValue(obj, key, data){
  obj[key] = data;
}


_robots.map(function(person) {
  return addKeyValue(person, 'isLiked', 'false');
});


public toggleNamedColor(indexID): void {
    _robots[indexID].isLiked = true;
}

希望对您有帮助。

答案 2 :(得分:1)

已在Ionic 4 Angular 8项目中实现:

HTML:

 <ion-slides pager="true" #slides>
    <ion-slide>
        <div>Slide page 1</div>
    </ion-slide>
    <ion-slide>
        <div>Slide page 2</div>
    </ion-slide>
    <ion-slide>
        <div>Slide page 3</div>
    </ion-slide>
 </ion-slides>

TypeScript:

 @ViewChild('slides', { static: true }) slides: IonSlides;

ngOnInit() {
    this.slides.ionSlidesDidLoad.subscribe((e) => {
        var swiperElements: HTMLCollection =
            this.elem.nativeElement.querySelectorAll(".swiper-pagination-bullet");
        var self = this;
        Array.from(swiperElements).forEach(function(element) {
            element.addEventListener("click", function() {
                self.goToSliderIndex(this, self);
            });
        });
    })
}

goToSliderIndex(child, self) {
    var parent = child.parentNode;
    var bulletIndex = Array.prototype.indexOf.call(parent.children, child);
    this.slides.slideTo(bulletIndex, 2000);
}