# angular2-useful-swiper

> Use iDangero.us's great slider [Swiper](http://idangero.us/swiper/#.V9C3w4VOLaI) in Angular.

Latest version **8.0.1-beta.1** (published 2019-06-27) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install angular2-useful-swiper
pnpm add angular2-useful-swiper
yarn add angular2-useful-swiper
bun add angular2-useful-swiper
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 8.0.1-beta.1 |
| Published | 2019-06-27 |
| First published | 2016-09-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 62.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 103 |
| Author | JayChase |
| Maintainers | jaychase |
| Keywords | Angular, swiper, slider, carousel |

## Links

- npm: https://www.npmjs.com/package/angular2-useful-swiper
- Repository: https://github.com/jaychase/angular2-useful-swiper
- Homepage: https://github.com/jaychase/angular2-useful-swiper#readme
- Issues: https://github.com/jaychase/angular2-useful-swiper/issues
- npm.io page: https://npm.io/package/angular2-useful-swiper

## Dependencies (1)

- [tslib](https://npm.io/package/tslib.md) ^1.9.0

## Alternatives

- [adhdev](https://npm.io/package/adhdev.md) — 11.1K weekly downloads
- [react-slide-button](https://npm.io/package/react-slide-button.md) — 310 weekly downloads
- [better](https://npm.io/package/better.md) — 42 weekly downloads
- [react-native-swipe-image](https://npm.io/package/react-native-swipe-image.md) — 22 weekly downloads
- [nl.fokkezb.pulltorefresh](https://npm.io/package/nl.fokkezb.pulltorefresh.md) — 11 weekly downloads

## Recent versions

- 8.0.1-beta.1 (latest) — 2019-06-27
- 8.0.1-alpha.1 (next) — 2019-06-18
- 8.0.1-beta — 2019-06-27
- 5.0.1 — 2017-10-23
- 5.0.0 — 2017-08-24
- 5.0.0-alpha.1 — 2017-08-24
- 5.0.0-alpha.0 — 2017-08-24
- 4.0.7 — 2017-08-24
- 4.0.7-alpha.1 — 2017-08-24
- 4.0.7-alpha — 2017-08-24
- 4.0.6 — 2017-07-12
- 4.0.5 — 2017-05-16
- 4.0.4 — 2017-05-05
- 4.0.3 — 2017-04-28
- 4.0.2 — 2017-04-27
- … 21 more at https://npm.io/package/angular2-useful-swiper/versions

## README

## angular2-useful-swiper

Use iDangero.us's great slider [Swiper](http://idangero.us/swiper/#.V9C3w4VOLaI) in Angular.

#### Quick links

[Swiper homepage](http://idangero.us/swiper/#.WTiywWiGNhE)

### Install

```bash
npm install --save angular2-useful-swiper@next swiper
npm install --save-dev @types/swiper
```

Add the swiper styles to the app styles in **angular.json**.

```json
{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "demo": {
      ...
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            ...
            "styles": [
              "./node_modules/swiper/dist/css/swiper.css"
            ],
            ...
```

### Usage

In **app.module.ts** (or in whichever child module you are using the component) import the **Angular2UsefulSwiperModule** module.

```typescript
@NgModule({
  declarations: [AppComponent],
  imports: [BrowserModule, Angular2UsefulSwiperModule],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule {}
```

Add the swiper component to your component to create a slider and add the content as you normally would to set up a slider (see the official [demos](http://idangero.us/swiper/demos/#.V9C73YVOLaI) for more information).
Note, you don't need to include the **swiper-container** div just the content, but the slides should be contained in a **swiper-wrapper** div and have the class **swiper-slide**.

```html
<my-component>
  <swiper [config]="config">
    <div class="swiper-wrapper">
      <div class="swiper-slide">Slide 1</div>
      <div class="swiper-slide">Slide 2</div>
      <div class="swiper-slide">Slide 3</div>
      <div class="swiper-slide">Slide 4</div>
      <div class="swiper-slide">Slide 5</div>
      <div class="swiper-slide">Slide 6</div>
      <div class="swiper-slide">Slide 7</div>
      <div class="swiper-slide">Slide 8</div>
      <div class="swiper-slide">Slide 9</div>
      <div class="swiper-slide">Slide 10</div>
    </div>
    <!-- Add Pagination -->
    <div class="swiper-pagination"></div>
    <!-- Add Arrows -->
    <div class="swiper-button-next"></div>
    <div class="swiper-button-prev"></div>
  </swiper>
</my-component>
```

Set the config for the swiper in you component and bind it to the component config property as above.

```javascript
export class MyComponent implements OnInit {
config: SwiperOptions = {
    pagination: { el: '.swiper-pagination', clickable: true },
    navigation: {
      nextEl: '.swiper-button-next',
      prevEl: '.swiper-button-prev'
    },
    spaceBetween: 30
  };
```

Set the height and width of the component.

```css
swiper {
  height: 300px;
  width: 400px;
}
```

The component also checks for the contents of swiper-wrapper being changed and calls update on the swiper when they are.
This allows for dynamic slide lists as you can see from the demo in this repo.

```html
<swiper [config]="config">
  <div class="swiper-wrapper">
    <img class="swiper-slide" *ngFor="let image of images" [src]="image" />
  </div>
  <div class="swiper-pagination"></div>
  <div class="swiper-button-next"></div>
  <div class="swiper-button-prev"></div>
</swiper>
```

**note for Bootstrap users**

To ensure the swiper works will with a column layout you may need to set the box-sizing to border-box on the swiper-wrapper.

```css
.swiper-wrapper {
  box-sizing: border-box;
}
```

#### Manually initializing the Swiper

By default the Swiper will be created in the **AfterViewChecked** event of the component. If the swiper is not going to have been rendered at this time (if it is on a hidden tab for example), it is best to handle the initialization manually.
To do this use the component's **initialize** property and only set it's value to true when ready. This will then initialize the Swiper the next time the next AfterViewChecked event is fired to ensure the DOM is ready.

```html
<mat-tabs mat-ripple mat-tab-active-index="0">
  <mat-tab-panel mat-tab-panel-title="Tab1"> </mat-tab-panel>
  <mat-tab-panel mat-tab-panel-title="Tab2" #panel>
    <swiper [config]="config" class="wrap1" [initialize]="panel.isActive">
      <div class="swiper-wrapper wrap1">
        <img
          class="swiper-slide"
          *ngFor="let image of trigger.images"
          [src]="image"
        />
      </div>
      <div class="swiper-pagination"></div>
      <div class="swiper-button-next"></div>
      <div class="swiper-button-prev"></div>
    </swiper>
  </mat-tab-panel>
</mat-tabs>
```

#### Accessing the Swiper instance

When a new instance of Swiper is created it is set as a property on the component. You can then access this by using a [template reference](https://angular.io/docs/ts/latest/guide/template-syntax.html#!#ref-vars).
For example add the template reference **#usefulSwiper**

```html
<swiper [config]="config" #usefulSwiper>
  <div class="swiper-wrapper">
    <img class="swiper-slide" *ngFor="let image of images" [src]="image" />
  </div>
  <div class="swiper-pagination"></div>
  <div class="swiper-button-next"></div>
  <div class="swiper-button-prev"></div>
</swiper>
```

..and then you can use the reference to access the **swiper** property.

```html
<button (click)="usefulSwiper.swiper.createLoop()">loop</button>
```

To access the swiper instance and all of it's properties, methods and events use a viewchild to get the component.swiper property.

```typescript
 @ViewChild('usefulSwiper',{static: false S}) usefulSwiper: SwiperComponent;

 ...

  next() {
    this.usefulSwiper.swiper.slideNext();
  }
```

---
_Source: https://npm.io/package/angular2-useful-swiper · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
