【问题标题】:Next and prev not working with angular2-Materialize carousel下一个和上一个不使用 angular2-Materialize 轮播
【发布时间】:2017-06-13 08:36:39
【问题描述】:

我想使用 next 和 prev 功能从一张幻灯片移动到另一张幻灯片。我不能使用 JQuery,所以 '$' 不是一个选项。我向我的轮播添加了一个 [materializeActions] 属性,并在我的组件中设置了事件发射器。我知道有些东西在起作用,因为当我调用 "actions.emit('next') 时,我没有收到错误;但是,当我调用 "actions.emit('badFunc') 时,我确实收到了错误。

此代码来自 angular2-materialize 提供的示例。我只添加了一个下一步移动按钮。

<div #carousel class="carousel" [ngClass]="{ 'initialized': showInitialized }"  materialize="carousel" [materializeActions]="actions">
<a *ngFor="let url of imageURLs" class="carousel-item"><img [src]="url"></a>
</div> 

<button (click)="next()">Next</button>

来自组件的代码:

import { Component, OnInit, ViewChild, EventEmitter } from '@angular/core';

@Component({
  selector: 'app-dashboard',
  templateUrl: './dashboard.component.html',
  styleUrls: ['./dashboard.component.css']
})
export class DashboardComponent implements OnInit {

  public modules: IModule[];

  @ViewChild('carousel') carouselElement;
  actions = new EventEmitter<string>();

  constructor() {

  }

 imageURLs = [
    'https://image.shutterstock.com/display_pic_with_logo/1264645/364153082/stock-photo-asian-girl-in-sunflower-field-364153082.jpg',
    'https://image.shutterstock.com/display_pic_with_logo/1264645/298927574/stock-photo-a-young-traveler-girl-sit-on-the-wooden-bridge-in-halong-bay-and-enjoy-the-beauty-of-seascape-298927574.jpg',
    'https://image.shutterstock.com/display_pic_with_logo/1264645/298757792/stock-photo-a-young-traveler-girl-sit-on-the-top-of-mountain-in-halong-bay-and-enjoy-the-beauty-of-seascape-298757792.jpg',
    'https://image.shutterstock.com/display_pic_with_logo/2565601/411902890/stock-photo-ha-long-bay-scenic-view-hanoi-vietnam-411902890.jpg',
    'https://image.shutterstock.com/display_pic_with_logo/2565601/413207668/stock-photo-the-temple-of-literature-in-hanoi-vietnam-the-chinese-words-is-poem-of-thie-temple-and-templs-s-413207668.jpg'
  ];

  ngOnInit() {

     // example of a hacky way to add an image to the carousel dynamically
    window.setTimeout(() => {
      this.imageURLs = [this.imageURLs[0], ...this.imageURLs]; // duplicate the first iamge
      this.carouselElement.nativeElement.classList.toggle('initialized');
      this.actions.emit('carousel');
    }, 1000);
  }

  next() {

    // Move to next slide. This is not working
    this.actions.emit('next');
  }

}

【问题讨论】:

    标签: angular angular2-material


    【解决方案1】:

    我找到了答案。它需要看起来像这样:

    this.actions.emit({ action: 'carousel', params: ['next'] });
    this.actions.emit({ action: 'carousel', params: ['prev'] });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-23
      • 1970-01-01
      • 2020-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-12
      相关资源
      最近更新 更多