0

I already tried these two methods -

ngAfterViewInit() or ngAfterContentInit()

Here is my code -

import { Component, OnInit } from '@angular/core';
import { HomepageService }  from '../services/homepage.service';
declare var $: any;

@Component({
  selector: 'app-detailpage',
  templateUrl: './detailpage.component.html',
  styleUrls: ['./detailpage.component.css']
})
export class DetailpageComponent implements OnInit {

  public errorMessage;
  public productdata;
  public title;
  public address;
  public old_price;
  public discount; 

   public     provider; 
   public     image;
   public     description;
   public     merchant;
   public     new_price;

 

  


  constructor(protected HomepageService: HomepageService) { 
  

   }


  ngOnInit() {

    this.HomepageService.getdetail() .then(
      data => { 
                 //console.log(data);
                 // let topdeals = <any>data ;
                  this.productdata = <any>data;                     

                  this.title = this.productdata.title;
                  this.address = this.productdata.address;
                  this.old_price = this.productdata.old_price;
                  this.discount = this.productdata.discount;

                   this.provider = this.productdata.provider;
                   this.image = this.productdata.image;
                   this.description = this.productdata.description;
                   this.merchant = this.productdata.merchant;
                   this.new_price = this.productdata.new_price;


      },
      error => { 
                 this.errorMessage = <any>error ;
                 //alert(this.errorMessage.message);  
      });

  }

  ngAfterViewInit() {
   

      var owl = $('.gallery .owl-carousel');
    owl.owlCarousel({
        loop: true,
        items: 1,
        thumbs: true,
        thumbImage: true,
        thumbContainerClass: 'owl-thumbs',
        thumbItemClass: 'owl-thumb-item'
    });


  }


}

In my html template i am passing image variable inside owl-carousel -

Problem is that my jquery code executed before even my angular 2 page rendered properly. I have face same issue with my other jquery scripts also. I do not know how to stop executing my custom jquery code before page completely rendered in angular 2.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Rajnesh Thakur
  • 326
  • 1
  • 3
  • 8

1 Answers1

0

Working plunk snatched form this answer. Component has following set-up:

import { Component, Input, ElementRef, HostBinding } from '@angular/core';
import $ from 'jquery';
import 'owl-carousel';

@Component({
  selector: 'owl-carousel',
  template: `<ng-content></ng-content>`
})
export class OwlCarousel {
  @HostBinding('class') defaultClass = 'owl-carousel';
  @Input() options: object;

  $owlElement: any;

  defaultOptions: any = {};

  constructor(private el: ElementRef) {}

  ngAfterViewInit() {
    // use default - empty
    // 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;
  }
}

Alternatively, you can use ng2-owl-carousel.

Both have DOM node passed by nativeElement not selector, so maybe this is the issuse.

//use this
this.$owlElement = $(this.el.nativeElement)
// instead of this
var owl = $('.gallery .owl-carousel');
Community
  • 1
  • 1