I'm trying to use owlcarousel 2 along with angular 4. I have the following setup:
.angular-cli.json
:
"scripts": ["../node_modules/jquery/dist/jquery.js",
"../node_modules/tether/dist/js/tether.js",
"../node_modules/bootstrap/dist/js/bootstrap.js",
"../node_modules/owl.carousel/dist/owl.carousel.js"
],
offers-component.ts
:
import { Component, AfterViewInit } from '@angular/core';
declare var $: any;
@Component({
selector: 'home-offers-container',
templateUrl: '../templates/home-offers-container.template.html',
styleUrls: ['../css/home-offers-container.component.css']
})
export class HomeOffersContainerComponent implements AfterViewInit {
ngAfterViewInit() {
$('.owl-carousel').owlCarousel();
}
}
template.html
<div class="row">
<div class="container">
<home-offers class="owl-carousel"></home-offers>
</div>
</div>
I get an error ERROR TypeError: $(...).owlCarousel is not a function
. I don't see why this error should pop up. I've ordered the scripts as it should be - first jquery then owlcarousel. Moreover typescritp does not give any error on jQuery. Am I missing anything / not loading owlCarousel properly?
Do I have to import jQuery / owlCarousel in the app.module.ts? If yes, any suggestions on how?
Using jQuery for DOM manipulation with Angular is not the right approach.
You can reach your DOM elements by using ViewChild decorator in this way:
and in your template class you got to specify who is that selector