您好我从Angular 2开始尝试让这个轮播插件工作:slick
过了一段时间,我设法让它像这样的组件一样工作:
import { Component, Input, ElementRef, AfterViewInit, AfterContentInit} from '@angular/core';
declare var jQuery: any;
@Component({
selector: 'slick-slider',
template: `
<ng-content></ng-content>
`
})
export class SlickSliderComponent implements AfterContentInit{
@Input() options: any;
$element: any;
defaultOptions: any = {};
constructor(private el: ElementRef) {
}
ngAfterContentInit() {
for (var key in this.options) {
this.defaultOptions[key] = this.options[key];
}
this.$element = jQuery(this.el.nativeElement).slick(this.defaultOptions);
}
}
我会在模板上使用它:
<slick-slider>
<div><img class="btn btn-lg" src="/assets/img/img1.png" /></div>
<div><img class="btn btn-lg" src="/assets/img/img2.png" /></div>
<div><img class="btn btn-lg" src="/assets/img/img3.png" /></div>
<div><img class="btn btn-lg" src="/assets/img/img4.png" /></div>
<div><img class="btn btn-lg" src="/assets/img/img5.png" /></div>
</slick-slider>
这很好用。但是当我尝试在* ng里面使用它时:
<slick-slider>
<div *ngFor="let source of sources">
<img class="btn btn-lg" (click)="watchSource(source)" src="/assets/img/{{source.name}}.png" />
</div>
</slick-slider>
它停止工作。我假设是因为滑块的init是在渲染<div>
元素之前完成的。因为我最终得到了这个渲染的html:
<slick-slider class="slick-initialized slick-slider">
<div>
<img ... >
</div>
<div>
<img ... >
</div>
<div>
<img ... >
</div>
<div>
<img ... >
</div>
<div aria-live="polite" class="slick-list draggable">
<div class="slick-track" role="listbox" style="opacity: 1; width: 0px; transform: translate3d(0px, 0px, 0px);">
<!-- Divs should be rendered here with slick styles included -->
</div>
</div>
</slick-slider>
知道如何使这项工作?我尝试使用&#34; ngAfterContentInit&#34;和&#34; ngAfterViewInit&#34;似乎都不起作用。
这是使用SlickSliderComponent的组件:
import { Component, OnInit } from '@angular/core'
import { DeviceSource } from './device-source'
import { RemoteService } from './remote.service';
@Component({
selector: 'device-selector',
moduleId: module.id,
templateUrl: 'device-selector.component.html',
providers: []
})
export class DeviceSelectorComponent implements OnInit {
sources: [DeviceSource];
ngOnInit(): void {
this.getDeviceSources();
}
constructor(private remoteService: RemoteService){}
getDeviceSources(): void {
this.remoteService.getDeviceSources().then(sources => this.sources = sources);
}
}
以下是使用的服务:
import { Injectable } from '@angular/core'
import { DeviceSource } from './device-source'
export const DEVICE_SOURCES:[DeviceSource]=
[
{id: 1, dispayName: 'TV', name:'tv'},
{id: 2, dispayName: 'Chrome', name:'chrome'},
{id: 3, dispayName: 'Cable', name:'cable'},
{id: 4, dispayName: 'XBox', name:'xbox'},
{id: 4, dispayName: 'Pi', name:'pi'},
];
@Injectable()
export class RemoteService {
getDeviceSources(): Promise<[DeviceSource]> {
return Promise.resolve(DEVICE_SOURCES);
}
}
答案 0 :(得分:12)
您应该向*ngIf
添加slick-slider
指令,以便在填充数据源后将其插入DOM,在这种情况下,它是变量sources
:
<slick-slider *ngIf="sources">
<div *ngFor="let source of sources">
<img class="btn btn-lg" (click)="watchSource(source)" src="/assets/img/{{source.name}}.png" />
</div>
</slick-slider>
答案 1 :(得分:0)
就我而言,使用* ngIf的技巧还不够。我在ngOnInit中调用REST服务并在订阅中接收响应我正在定义数组。 对我来说唯一可行的解决方法是直接从typescript手动添加光滑的功能'slickAdd'。
jQuery('.my-slick').slick('slickAdd', '<div><h3>ahoj</h3></div>');
也许它会帮助某人......