如何在 Angular2 中使用 owl-carousel? - IT屋-程序员软件开发技术分享社区
- 首页
- 其他开发
- 如何在 Angular2 中使用 owl-carousel?
如何在 Angular2 中使用 owl-carousel?
[英] How to use owl-carousel in Angular2?
本文介绍了如何在 Angular2 中使用 owl-carousel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是 Angular2 的新手,并试图将 Angularjs 中的 owl-carousel 转换为 Angular2.
下面是 owl-carousel 实现的 index.html 文件:
<html ng-app="plunker"><头><meta charset="utf-8"/><title>AngularJS Plunker</title><script>document.write('<base href="' + document.location + '"/>');</script><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/owl-carousel/1.3.3/owl.carousel.min.css"/><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/owl-carousel/1.3.3/owl.theme.min.css"/><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/owl-carousel/1.3.3/owl.transitions.min.css"/><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/owl-carousel/1.3.3/owl.carousel.min.js"/><script data-require="angular.js@1.3.x" src="https://code.angularjs.org/1.3.15/angular.js" data-semver="1.3.15"></脚本><script data-require="jquery@2.1.3" data-semver="2.1.3" src="http://code.jquery.com/jquery-2.1.3.min.js"></脚本><script src="https://cdnjs.cloudflare.com/ajax/libs/owl-carousel/1.3.3/owl.carousel.min.js"></script><script src="app.js"></script>头部><body ng-controller="MainCtrl"><data-owl-carousel class="owl-carousel" data-options="{navigation: true, pagination: false, rewindNav: false}"><div owl-carousel-item="" ng-repeat="item in ::items1" class="item"><p>{{::item}}</p>
</data-owl-carousel><data-owl-carousel class="owl-carousel" data-options="{navigation: false, pagination: true, rewindNav: false}"><div owl-carousel-item="" ng-repeat="item in ::items2" class="item"><p>{{::item}}</p>
</data-owl-carousel>
</html>
这是 app.js 文件:
var app = angular.module('plunker', []);app.controller('MainCtrl', function($scope) {$scope.items1 = [1,2,3,4,5];$scope.items2 = [1,2,3,4,5,6,7,8,9,10];}).directive("owlCarousel", function() {返回 {限制:'E',转置:假,链接:功能(范围){scope.initCarousel = 函数(元素){//提供你想要的任何默认选项var defaultOptions = {};var customOptions = scope.$eval($(element).attr('data-options'));//合并两个选项对象for(customOptions 中的 var 键){defaultOptions[key] = customOptions[key];}//初始化轮播$(element).owlCarousel(defaultOptions);};}};}).directive('owlCarouselItem', [function() {返回 {限制:'A',转置:假,链接:函数(范围,元素){//等待 ng-repeat 中的最后一项,然后调用 init如果(范围.$last){scope.initCarousel(element.parent());}}};}]);
这是 Angular1 的实现,如何为 Angular2 实现?
解决方案
更新
OwlCarousel2 + Angular2.3.0
ngOnDestroy() {this.$owlElement.owlCarousel('destroy');this.$owlElement = null;}
旧版本
以下是 angular2 owl-carousel 实现的 app.ts 文件:
import {Component} from 'angular2/core';从 './owl-carousel.component' 导入 { OwlCarousel };@成分({选择器:'应用',指令:[OwlCarousel],模板:`<h2>样品1</h2><owl-carousel [options]="{navigation: true, pagination: false, rewindNav: false}"><div *ngFor="#item of items1"><p>{{ item }}</p></owl-carousel><h2>样品2</h2><owl-carousel [options]="{navigation: false, pagination: true, rewindNav: false}"><div *ngFor="#item of items2"><p>{{ item }}</p>
</owl-carousel><h2>样品3</h2><owl-carousel [options]="{navigation: false, pagination: true, rewindNav: false}"><div *ngFor="#img of images"><img src="http://lorempixel.com/400/200/{{img}}"/>
</owl-carousel>`})出口类应用{items1: 数组 = [1, 2, 3, 4, 5];items2: 数组 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];图像:数组 = ['体育'、'抽象'、'人'、'交通'、'城市'、'技术'、'夜生活'、'动物'];}
owl-carousel.component.ts
import { Component, Input, ElementRef } from 'angular2/core';从'jquery'导入$;导入猫头鹰旋转木马";@成分({选择器:'猫头鹰旋转木马',模板:`<ng-content></ng-content>`})导出类 OwlCarousel {@Input() 选项:对象;$owlElement: 任何;默认选项:对象 = {};构造函数(私有 el:ElementRef){}ngAfterViewInit() {for (this.options 中的 var 键) {this.defaultOptions[key] = this.options[key];}this.$owlElement = $(this.el.nativeElement).owlCarousel(this.defaultOptions);}ngOnDestroy() {this.$owlElement.data('owlCarousel').destroy();this.$owlElement = null;}}
您可以在 plunker
中看到完整示例
OwlCarousel2 版本在这里 https://plnkr.co/edit/FnZVxB?p=预览
I am new to Angular2 and was trying to convert owl-carousel in Angularjs to Angular2.
Below is the index.html file for the owl-carousel implementation:
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/owl-carousel/1.3.3/owl.carousel.min.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/owl-carousel/1.3.3/owl.theme.min.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/owl-carousel/1.3.3/owl.transitions.min.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/owl-carousel/1.3.3/owl.carousel.min.js" />
<script data-require="angular.js@1.3.x" src="https://code.angularjs.org/1.3.15/angular.js" data-semver="1.3.15"></script>
<script data-require="jquery@2.1.3" data-semver="2.1.3" src="http://code.jquery.com/jquery-2.1.3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/owl-carousel/1.3.3/owl.carousel.min.js"></script>
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
<data-owl-carousel class="owl-carousel" data-options="{navigation: true, pagination: false, rewindNav : false}">
<div owl-carousel-item="" ng-repeat="item in ::items1" class="item">
<p>{{::item}}</p>
</div>
</data-owl-carousel>
<data-owl-carousel class="owl-carousel" data-options="{navigation: false, pagination: true, rewindNav : false}">
<div owl-carousel-item="" ng-repeat="item in ::items2" class="item">
<p>{{::item}}</p>
</div>
</data-owl-carousel>
</body>
</html>
Here is the app.js file:
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.items1 = [1,2,3,4,5];
$scope.items2 = [1,2,3,4,5,6,7,8,9,10];
}).directive("owlCarousel", function() {
return {
restrict: 'E',
transclude: false,
link: function (scope) {
scope.initCarousel = function(element) {
// provide any default options you want
var defaultOptions = {
};
var customOptions = scope.$eval($(element).attr('data-options'));
// combine the two options objects
for(var key in customOptions) {
defaultOptions[key] = customOptions[key];
}
// init carousel
$(element).owlCarousel(defaultOptions);
};
}
};
}).directive('owlCarouselItem', [function() {
return {
restrict: 'A',
transclude: false,
link: function(scope, element) {
// wait for the last item in the ng-repeat then call init
if(scope.$last) {
scope.initCarousel(element.parent());
}
}
};
}]);
This is the Angular1 implementation how to implement it for Angular2?
解决方案
Update
OwlCarousel2 + Angular2.3.0
ngOnDestroy() {
this.$owlElement.owlCarousel('destroy');
this.$owlElement = null;
}
Old version
Below is the app.ts file for the angular2 owl-carousel implementation:
import {Component} from 'angular2/core';
import { OwlCarousel } from './owl-carousel.component';
@Component({
selector: 'app',
directives: [OwlCarousel],
template: `
<h2>Sample 1</h2>
<owl-carousel [options]="{navigation: true, pagination: false, rewindNav : false}">
<div *ngFor="#item of items1">
<p>{{ item }}</p>
</div>
</owl-carousel>
<h2>Sample 2</h2>
<owl-carousel [options]="{navigation: false, pagination: true, rewindNav : false}">
<div *ngFor="#item of items2">
<p>{{ item }}</p>
</div>
</owl-carousel>
<h2>Sample 3</h2>
<owl-carousel [options]="{navigation: false, pagination: true, rewindNav : false}">
<div *ngFor="#img of images">
<img src="http://lorempixel.com/400/200/{{img}}"/>
</div>
</owl-carousel>`
})
export class App {
items1: array = [1, 2, 3, 4, 5];
items2: array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
images: array = ['sports', 'abstract', 'people', 'transport', 'city', 'technics', 'nightlife', 'animals'];
}
owl-carousel.component.ts
import { Component, Input, ElementRef } from 'angular2/core';
import $ from 'jquery';
import 'owl-carousel';
@Component({
selector: 'owl-carousel',
template: `<ng-content></ng-content>`
})
export class OwlCarousel {
@Input() options: object;
$owlElement: any;
defaultOptions: object = {};
constructor(private el: ElementRef) {}
ngAfterViewInit() {
for (var key in this.options) {
this.defaultOptions[key] = this.options[key];
}
this.$owlElement = $(this.el.nativeElement).owlCarousel(this.defaultOptions);
}
ngOnDestroy() {
this.$owlElement.data('owlCarousel').destroy();
this.$owlElement = null;
}
}
Full example you can see in plunker
OwlCarousel2 version is here https://plnkr.co/edit/FnZVxB?p=preview
这篇关于如何在 Angular2 中使用 owl-carousel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文