简体   繁体   English

下一个和上一个不适用于angular2-Materialize轮播

[英]Next and prev not working with angular2-Materialize carousel

I want to move from slide to slide using the next and prev functions. 我想使用next和prev函数在幻灯片之间移动。 I can't use JQuery so '$' is not an option. 我无法使用JQuery,因此不能使用“ $”。 I added a [materializeActions] attribute to to my carousel and setup the event emitter in my component. 我向轮播添加了[materializeActions]属性,并在组件中设置了事件发射器。 I know something is working a little because when I call "actions.emit('next') I don't get an error; however, when I call "actions.emit('badFunc') I do get an error. 我知道有些事情在起作用,因为当我调用“ actions.emit('next')时,我不会收到错误;但是,当我调用” actions.emit('badFunc')时,我确实会收到错误。

This code is from the sample that is provided with angular2-materialize. 该代码来自angular2-materialize提供的示例。 All I added was a button to move next. 我添加的只是一个下一步移动的按钮。

<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>

Code from the component: 来自组件的代码:

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');
  }

}

I found the answer. 我找到了答案。 It needs to look like this: 它需要看起来像这样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM