Is it Beneficial to Combine jQuery with TypeScript in Angular 7?

Our school project team is venturing into the world of Angular and TypeScript, but we all admit to being newbies in this area.

I've mainly focused on the design aspect of our project and left the coding to my teammates. However, I recently completed my part and did a git pull. That's when I stumbled upon some code written by one of my friends:

ngOnInit() {
    $(document).ready(function() {
      $(".dropdown-trigger").dropdown();
      $(".hamburger-nav").on("click", function() {
        setHamburgerMenu();
        $(".menu").animate({
          height: 'toggle'
        });
      });
      
      $('.modal').modal({
        inDuration: 400,
        outDuration: 400,
        preventScrolling: false
      });
    }); //document ready function
  }

Although we are using Materialize CSS for styling, I can't shake off this feeling of unease about seeing jQuery code in an Angular 2 application. It just seems strange to have $.ready following an Angular NgOnInit. Moreover, shouldn't Angular 2 provide event handling functionality that eliminates the need for $.on('click', ...)?

So, is this piece of code correct or incorrect? Also, when it comes to TypeScript, is it considered good practice (or even acceptable) to mix jQuery in a TypeScript file like this?

I apologize if these questions seem basic. As someone who is brand new to Angular and has only completed an EdX course on the subject, I've been scouring the internet unsuccessfully for answers. I'm simply eager to learn more!

Answer №1

While it is possible, Angular does not prioritize direct DOM manipulation like JQuery. Instead, Angular encourages a different approach to working with the DOM. You can achieve anything in Angular that you would typically use JQuery for. It's recommended to explore Angular's features and functionalities when you need to perform certain tasks, and try to avoid directly manipulating the DOM using either JQuery or JavaScript/TypeScript.

For more information, check out Angular Docs Fundamentals.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

When using Laravel with jQuery Ajax, the search for the file address is unsuccessful

Learning Laravel has been a challenging journey for me. I've encountered numerous problems that have made it feel like more of a hindrance than a helpful tool. But the real story lies elsewhere. Working with Laravel 4 and jQuery 2, I attempted to fet ...

Incorporate FontAwesome icons into table headers within an Angular framework

I am attempting to customize the icons for sorting in my table headers by following the guidelines laid out in the ng-bootstrap table tutorial. The NgbdSortableHeader directive plays a key role in changing the sorting of columns: @Directive({ selector: ...

Box Pattern with Pop-Up Modal

Check out the grid of squares I've coded below: <div class='square-container'> <div class='square'></div> <div class='square'></div> <div class='square'></div& ...

activating serverless.yml for aws-xray

I have been attempting to implement AWS X-Ray for all lambda functions in the following manner: serverless.yml provider: tracing: lambda: true apiGateway: true name: aws runtime: nodejs8.10 stage: ${opt:stage, 'dev'} region: ...

Trouble with JSON.stringify() object in AJAX request within Codeigniter framework

let dataString = JSON.stringify(formData); console.log(dataString); $.ajax({ url: urL, type: "POST", cache: false, data: dataString, success: function (data) { console.log(data); } }); When executing the code above, the ...

The duplication of printed keys and values from a JavaScript object is causing issues

I am working with a JSON object structured like this: var data = { "2020-01-20": ["08:00 - 09:00", "09:00 - 10:00"], "2020-01-21": ["08:00 - 09:00"] }; My objective is to display each value along with its corresponding key in a list format, as follow ...

Creating a dedicated class or module specifically designed for handling import and export tasks is a highly efficient approach towards stream

Exploring different ways to import multiple classes into a single class file: import myClass1 'pathto1'; import myClass2 'pathto2'; import myClassn 'pathton'; Seeking a simpler method using one file (class export) with al ...

Retrieve all colors from the style attribute

I'm on the hunt for a method to extract all CSS colors from a website. Currently, I have been able to manage internal and external stylesheets by utilizing document.styleSheets. However, my issue lies in the fact that any styles directly assigned to a ...

What is the method for obtaining the current participant's ID from the Angular frontend of a Hyperledger Composer application?

Need help with hyperledger-composer: I know how to retrieve the id of the current participant within a transaction processor function using this code: let currentParticipant = getCurrentParticipant(); let participantId = currentParticipant.getFullyQuali ...

Utilizing a Variable Value to Invoke a Directive in Angular 2+

I am currently working with Material Angular, and I have the standard button as follows: <button mat-button> x </button> or <button mat-raised-button> x </button> Let's say I have a string variable named 'type', wh ...

After a recent deployment, the Angular PWA hosted on a NestJS server experiences unexpected crashes

I have set up an Angular PWA (version 11) on a NestJS (version 7) server. However, after each new deployment, the PWA crashes because the browser attempts to fetch a JavaScript file that no longer exists and then redirects to the root site (html): main.945 ...

Observable dataset

I'm curious about the correct type of Observables array. Can you advise? So far, I've attempted the following: let myObservables: Observable[] = new Array(); let myObservables: Observable<Array<any>> = new Array(); let myObservables ...

Save the chosen information into the database

My goal is to insert a Foreign key, acc_id, into the patient_info table from the account_info table. I have successfully retrieved the data from my database and now I want to use it as a Foreign key in another table. Here is the code snippet: try { $ ...

Establishing a pre-defined value for a filter that impacts an HTML table upon the page's initialization

table: <input id="weekFilter" type="text" name="myInputSearches" placeholder="Week..." style="margin-top:4px"> <label class="noResults" align="right" style="display:none; color:red"><b><i>No Match Found</i></b></labe ...

Ensuring a component is included in a module or nesting a component within a template

Struggling with Angular (not AngularJS) and facing an issue where my template is not referencing components correctly. It seems like the component is not being recognized, either due to incorrect declaration or importing in the right place. I need guidanc ...

Animating Text Changes with JavaScript and jQuery

In my HTML, I have an input type="text" element and I want to attach an event handler that triggers when the text is changed. The issue arises because this input's value is dynamically updated by another JavaScript function, causing the event handler ...

Is there a way to incorporate the "Handoff to Human" feature in a Microsoft Teams bot app by utilizing the Teams Toolkit? Can this functionality be implemented using TypeScript?

Can someone assist me with figuring out how to incorporate the "handoff conversation to human agent mechanism" in my project using TypeScript, Microsoft Bot Framework, and Teams Toolkit? ...

Utilizing Piwik Analytics in jQuery Mobile Framework

Having an issue with tracking users on my mobile Web App using Piwik. Due to AJAX, only views on the first page are being tracked. I attempted to use the pageinit function to load the Piwik tracking script on every page, but it still only tracks the firs ...

Lack of y data in the Highcharts

I am facing an issue with retrieving yAxis data in Highcharts. You can view the fiddle at https://jsfiddle.net/LLExL/6496/. I have loaded Highcharts using the code below. $(function () { $('#RankingReportsHistory').highcharts( ...

Update the content of the document element by assigning it a lengthy string

I'm utilizing JQuery to dynamically assign content to a div element. The content has numerous lines with specific spacing, so this is the approach I am taking: document.getElementById('body').innerHTML = "Front-End Developer: A <br/> ...