所以我有这个使用materializecss javascript的滑块组件。所以在渲染之后,我需要执行
$(document).ready(function(){
$('body').on('Slider-Loaded',function(){
console.log('EVENT SLIDER LOADED');
$('.slider').slider({full_width: true});
$('.slider').hover(function () {
$(this).slider('pause');
}, function () {
$(this).slider('start')
});
});
});
但我不太清楚在哪里放置这一行,因为 typescript/angular 删除模板中的脚本标记。我已经在 ts 文件中包含了 JQuery,并希望使用事件系统,但这似乎不起作用。有任何想法吗?这是ts文件的代码:
/// <reference path="../../typings/main.d.ts" />
import {Component, Input} from 'angular2/core';
import { RouterLink } from 'angular2/router';
import {ServerService} from './server';
@Component({
selector: 'slider',
templateUrl:'/app/template/slider.html',
directives: [ ],
})
export class SliderComponent {
@Input() images;
private server: ServerService;
public constructor(server: ServerService){
this.server = server;
}
ngOnInit() {
console.log("THROWING EVENT");
$('body').trigger('Slider-Loaded');
}
}
原文由 Carl Boisvert 发布,翻译遵循 CC BY-SA 4.0 许可协议
ngAfterViewInit()
ofAppComponent
是在根组件及其子组件被渲染后的生命周期回调 Angular 调用,它应该适合您的目的。另请参阅 https://angular.io/guide/lifecycle-hooks