简体   繁体   English

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

[英]Execute javascript/jquery code after angular2 finish template rendering

I already tried these two methods - 我已经尝试过这两种方法-

ngAfterViewInit() or ngAfterContentInit() ngAfterViewInit()或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 - 在我的html模板中,我将图像变量传递给owl-carousel-

Problem is that my jquery code executed before even my angular 2 page rendered properly. 问题是我的jquery代码在正确显示角度2页面之前就已执行。 I have face same issue with my other jquery scripts also. 我的其他jquery脚本也遇到相同的问题。 I do not know how to stop executing my custom jquery code before page completely rendered in angular 2. 我不知道如何在角度2中完全呈现页面之前停止执行我的自定义jquery代码。

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 . 或者,您可以使用ng2-owl-carousel

Both have DOM node passed by nativeElement not selector, so maybe this is the issuse. 两者都有由nativeElement而不是选择器传递的DOM节点,所以也许这就是问题。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM