1

I need to convert the following jquery codes to typescript code.

Jquery

$(".room").click({
    console.log("clicked");
});

TypeScript

import { Component } from '@angular/core';
declare var $: any;

export class AppComponent {

}
3
  • 3
    What stops you from doing so ? Commented Nov 28, 2017 at 8:30
  • 1
    What @Rayon said + I dont see a question at all. Commented Nov 28, 2017 at 8:31
  • Because it is not recomended to use jQuery inside angular project Commented Nov 28, 2017 at 8:38

1 Answer 1

3

On your element that has room class in HTML you can do something like this in your component template:

For example if your element is a button:

<button class="room" #btn (click)="buttonClick()">Click Me!</button>

And then in your class:

import { Component } from '@angular/core';
declare var $: any;

export class AppComponent {
    buttonClick(){
        console.log("Button Clicked!");
    }
}

Hope this helps :)

Sign up to request clarification or add additional context in comments.

2 Comments

Your answer is ok to me, but i do not want to wrap it in angular side. i need it globally of full page.
It is not advisable to have events outside component scope. You have an option to do this on your appComponent, which is the parent most component. That will make it global.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.