我正在尝试使用Angular 2对旋转木马/滑块进行编码。我的想法是,我有一系列图像,
export class AppComponent {
title = 'app works!';
images: Array<any> = [
{
image: "1.jpg"
},
{
image: "2.jpg"
},
{
image: "3.jpg"
},
{
image: "4.jpg"
},
{
image: "5.jpg"
},
{
image: "6.jpg"
}
];
}
我想遍历图像数组并用当前值{{images.image}}
替换div的背景图像。
我已经设置了一个柜台,
public subscription: any;
public time: any;
ngOnInit() {
let timer = TimerObservable.create(1000,5000);
this.subscription = timer.subscribe(t=>{
this.time = t;
})
}
并且目标div
看起来像
<div class="slider__home" style="background: url({{image}})">
</div>
我如何实现这一目标?我不想使用bootstrap或jQuery,但只是简单的Angular2,感谢您的帮助。
答案 0 :(得分:1)
像这样:
<div class="slider__home" [style.background]="'url('+ image +')'">
</div>
答案 1 :(得分:1)
你可以像这样使用bootstrap轮播:
的index.html
<html>
<head>
<!-- Insert all css data here -->
</head>
<body>
<carousel></carousel>
<!-- Insert all needed scripts here -->
<script>
System.import('carousel.js');
</script>
</body>
</html>
carousel.html
<div id="carousel-generic" class="carousel slide" data-ride="carousel">
<div class="carousel-inner" role="listbox">
<div *ngFor="let url of urls" class="item" [ngClass]="{active: isActive(url)}">
<img src="{{url}}" alt="{{url}}">
</div>
</div>
<!-- Controls -->
<a class="left carousel-control" href="#carousel-generic" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#carousel-generic" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
carousel.ts
// <reference path="jquery.d.ts" />
// <reference path="boodstrap.d.ts" />
import {Component} from '@angular/core';
import {Http} from '@angular/http';
@Component({
selector: "carousel",
templateUrl: 'carousel.html'
})
export class CarouselComponent {
private start = false;
urls = [];
constructor(http: Http) {
http.get('/getcarousel')
.subscribe(res => this.startCarousel(res.json()));
}
startCarousel(urls: string[]) {
this.urls = urls;
$('.carousel').carousel();
}
isActive(url: string) {
return url === this.urls[0];
}
}
答案 2 :(得分:0)
我刚刚为this question写了一个类似于你问题的答案。
如果您想了解如何从头开始创建内容滑块here on by blog,则可以很好地解释。您将能够以纯角度2和css创建一个轻量级滑块,它将与下面的示例一样简单。
typescript
@Component({
selector: 'ImageShow',
template: `
<contentSlider [slides]="images"></contentSlider>
`
})
export class ImageShowComponent implements AfterViewInit{
images:Array<any> = [{"sType":"img","imgSrc":"..."},{"sType":"div","content":"...Hello It's slidable content"}];
constructor(){
}
}