[英]Run jQuery code after render template in Angular 2
I have component: 我有组件:
@Component({
selector: 'fx-app',
templateUrl: 'app/modules/main/page.html',
directives: [RouterOutlet,RouterLink,ROUTER_DIRECTIVES],
providers: [
ROUTER_PROVIDERS,
HTTP_PROVIDERS
]
})
export class MainComponent {
constructor(){
var menu = new MainMenu();
}
}
With its template app/modules/main/page.htm
: 使用模板app/modules/main/page.htm
:
<div id="menu">
<div class="logo"></div>
<div class="menuContainer">
<ul>
<li>
<a class="withNested closed">
<span class="word">Users</span>
</a>
</li>
<li>
<a [routerLink]="['Products']">
<span class="word">Products</span>
</a>
</li>
</ul>
</div>
</div>
<div id="content">
<router-outlet></router-outlet>
</div>
<div id="footer"></div>
Class MainMenu
looks like this: MainMenu
类看起来像这样:
export class MainMenu{
public menu = null;
public allLinks = null;
constructor(){
this.menu = $('#menu');
this.allLinks = $('#menu').find('a');
this.menu.find('ul a').click(this.setItem);
console.log('init MainMenu');
}
public setItem (){
this.allLinks.removeClass('active');
$(this).addClass('active');
if($(this).hasClass('withNested')){
if($(this).hasClass('opened')){
$(this).removeClass('opened');
$(this).addClass('closed');
$(this).next('ul').slideUp();
}else{
$(this).removeClass('closed');
$(this).addClass('opened');
$(this).next('ul').slideDown();
}
}
}
}
Code of class MainMenu
does not work, because it works before the displayed template app/modules/main/page.htm
. MainMenu
类的代码不起作用,因为它在显示的模板app/modules/main/page.htm
。 How to make that jQuery-code of class MainMenu
started to work correctly? 如何使MainMenu
类的jQuery代码开始正常工作?
I think you need to use ngOnInit() as it fires as soon as the dom is ready: 我认为你需要使用ngOnInit(),因为它会在dom准备好后立即触发:
import {OnInit} from 'angular2/core';
export class MainMenu implements OnInit {
public menu = null;
public allLinks = null;
constructor(){
}
ngOnInit() {
this.menu = $('#menu');
this.allLinks = $('#menu').find('a');
this.menu.find('ul a').click(this.setItem.bind(this));
console.log('init MainMenu');
}
public setItem (e){
this.allLinks.removeClass('active');
$(e.currentTarget).addClass('active');
if($(e.currentTarget).hasClass('withNested')){
if($(e.currentTarget).hasClass('opened')){
$(e.currentTarget).removeClass('opened');
$(e.currentTarget).addClass('closed');
$(e.currentTarget).next('ul').slideUp();
}else{
$(e.currentTarget).removeClass('closed');
$(e.currentTarget).addClass('opened');
$(e.currentTarget).next('ul').slideDown();
}
}
}
}
You need to use one of the events available on a component life cycle , a good resource is http://learnangular2.com/lifecycle/ . 您需要使用组件生命周期中可用的事件之一,一个好的资源是http://learnangular2.com/lifecycle/ 。
By the way, just a note, you are using jQuery for DOM access and you shouldn't do that. 顺便说一句,只是一个注释,你正在使用jQuery进行DOM访问,你不应该这样做。 You should stay in the Angular Environment, try to avoid accessing dom directly. 你应该留在角度环境中,尽量避免直接访问dom。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.