在angular2完成模板渲染后执行javascript / jquery代码

时间:2017-05-05 14:13:22

标签: javascript angular

我已经尝试过这两种方法 -

ngAfterViewInit()或ngAfterContentInit()

这是我的代码 -



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'
    });


  }


}
&#13;
&#13;
&#13;

在我的html模板中,我在owl-carousel中传递图像变量 -

问题是我的jquery代码在我的角度2页面正确渲染之前执行。我的其他jquery脚本也遇到了同样的问题。我不知道如何在以角度2完全呈现页面之前停止执行我的自定义jquery代码。

1 个答案:

答案 0 :(得分:0)

工作plunk抓取表格this answer。组件具有以下设置:

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;
  }
}

或者,您可以使用ng2-owl-carousel

两者都有nativeElement传递的DOM节点而不是选择器,所以这可能就是issuse。

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