ボタンをクリックしなくてもJQuery関数を実行できるようにするには、いくつかのヘルプを使用できます。今すぐクリックするボタンがある場合にのみ機能するので、JQueryが起動します。Angular2 JQuery、document.ready関数の作り方
コード
declare var jQuery: any;
@Component({
selector: 'home-component',
providers: [],
moduleId: module.id,
encapsulation: ViewEncapsulation.None,
templateUrl: 'home.component.html',
styleUrls: ['home.component.css'],
directives: [BannerComponent],
})
export class HomeComponent implements OnInit {
constructor(public productService: ProductService, private elref: ElementRef) {
}
ngOnInit(): any {
console.log("jQuery here");
jQuery(this.elref.nativeElement).find('button').on('click', function() {
jQuery('#owl-example').owlCarousel();
});
}
}
このコードは、コンポーネントの内部にOFCれます。ボタンを見つけ、クリックするとjQuery関数が実行されます。 ngOnInitが(いずれかのボタンをクリックせずに)と呼ばれている
jQuery('#owl-example').owlCarousel();
火災:私が欲しいもの
はこのラインを作ることです。そうするために、私はjQueryのを実装する必要があります。
$(document).ready(function() { }
ここでの問題は、それは私がこれまで試してみましたが、このコードの平和です:)動作しないということである。
ngOnInit(): any {
console.log("jQuery here");
jQuery(this.elref.nativeElement).ready(function() {
jQuery('#owl-example').owlCarousel();
});
}
誰かがことを願って助けて。
'jQuery( '#owl-example')。owlCarousel();'このようなことは絶対にしないでください。 – dfsq
なぜそれをしないのですか? – Mikkel
CSSセレクタをハードコーディングしたので、柔軟性がありません。 – dfsq