angular - *ngFor and *ngIf are not working with ngx-slick-carousel in angular. How to fix it?
问题描述
I am making carousel using ngx-slick-carousel in angular. It works fine with static data but when I try to use *ngFor in the div instead of static data then it displays all the div in verticle line instead of horizontal. How to solve this issue ?
app.component.html
<div class="slider slider-nav">
<div *ngFor="let i of numberArray">
<h3>{{i}}</h3>
</div>
</div>
<div class="action">
<a href="#" data-slide="3">go to slide 3</a>
<a href="#" data-slide="4">go to slide 4</a>
<a href="#" data-slide="5">go to slide 5</a>
</div>
</div>
// ---------------------------------
/* this shows perfect result
<div class="main">
<div class="slider slider-nav">
<div><h3>1</h3></div>
<div><h3>2</h3></div>
<div><h3>3</h3></div>
<div><h3>4</h3></div>
<div><h3>5</h3></div>
<div><h3>6</h3></div>
<div><h3>7</h3></div>
</div>
<div class="action">
<a href="#" data-slide="3">go to slide 3</a>
<a href="#" data-slide="4">go to slide 4</a>
<a href="#" data-slide="5">go to slide 5</a>
</div>
</div>*/
// -----------------------------
In app.component.scss file
.main {
font-family:Arial;
width:500px;
display:block;
margin:0 auto;
}
h3 {
background: yellow;
color: #3498db;
font-size: 36px;
line-height: 100px;
margin: 10px;
padding: 2%;
position: relative;
text-align: center;
}
.action{
display:block;
margin:100px auto;
width:100%;
text-align:center;
}
.action a {
display:inline-block;
padding:5px 10px;
background:#f30;
color:#fff;
text-decoration:none;
}
.action a:hover{
background:#000;
}
In app.component.ts file
import { Component, OnInit } from '@angular/core';
declare var $: any;
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
title = 'slickApp';
numberArray = [1, 2, 3, 4, 5, 6, 7, 8, 9];
setCarousel() {
$('.slider-nav').slick({
slidesToShow: 4,
slidesToScroll: 1,
dots: true,
focusOnSelect: true
});
$('a[data-slide]').click((e) => {
e.preventDefault();
const slideno = $(this).data('slide');
$('.slider-nav').slick('slickGoTo', slideno - 1);
});
}
ngOnInit() {
setTimeout(() => {
this.setCarousel();
}, 5000);
}
}
In app.module.ts file
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { SlickCarouselModule } from 'ngx-slick-carousel';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
SlickCarouselModule,
AppRoutingModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
In angular.json file I have added following things also
"styles": [
"src/styles.scss",
"node_modules/slick-carousel/slick/slick.scss",
"node_modules/slick-carousel/slick/slick-theme.scss"
],
"scripts": [
"node_modules/jquery/dist/jquery.min.js",
"node_modules/slick-carousel/slick/slick.min.js"
],
How can i solve this issue ?
解决方案
推荐阅读
- javascript - Native d3.js v5 - 构建具有所需间隔的径向刻度
- tensorflow - Tensorflow gpu 错误:Dst 张量未初始化
- asp.net-core - 如何在开放层 v6 中使用离线地图
- python - 导入带有空格分隔符格式的txt文件
- ios - 关键帧动画后图像消失
- python - 如何删除 Python 中所有列表项的一部分?
- ios - Cocoapods - 为私有 iOS 存储库创建 pod
- javascript - WebExtension onBeforeReuest 不会触发来自 fetch API 的请求
- javascript - javascript ES6 / 如何正确链接 Promises 并实现处理
- react-native - React redux - 将参数传递给 url - 错误 - 动作必须是普通对象