How to call jquery functions from angular?











up vote
1
down vote

favorite












I'm starting with angular 6 and I'm a bit confuse about how I can call a jquery function.



This is my app.conponent.html:



<div id="mySidenav" class="sidenav">
<a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
<a href="#">About</a>
<a href="#">Services</a>
<a href="#">Clients</a>
<a href="#">Contact</a>
</div>
<div class="main">
<nav class="navigator">
<ul class="nav nav-pills">
<li class="nav-item">
<a class="nav-link" onclick="openNav()" href="#"><i class="material-icons">menu</i></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Active</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
</ul>
</nav>
</div>
<router-outlet></router-outlet>


An this is my app.component.ts:



import { Component, OnInit  } from '@angular/core';
import * as $ from 'jquery';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
title = 'Look jQuery Animation working in action!';

public ngOnInit()
{
$(document).ready(function(){
$('#mySidenav').click(function(){
document.getElementById("mySidenav").style.width = "250px";
document.getElementById("main").style.marginLeft = "250px";
});
$('#mySidenav').click(function(){
document.getElementById("mySidenav").style.width = "250px";
document.getElementById("main").style.marginLeft = "250px";
});
});
}
}


If i use the tag $('div').click(function(){ and only one function, this works as expected. But I guess, I need to distinct each different id from its respective function.



What is the best practice to call a jQuery function from angular?



Thanks for your time.



Kind regards.










share|improve this question
























  • It not a really good pratice to use jQuery with angular. Take a look at that post : stackoverflow.com/questions/34707577/…
    – veben
    Nov 20 at 15:56










  • angularjs and angulario have their own patterns for binding event handlers to elements. You don't want to use jQuery for these operations.
    – Taplar
    Nov 20 at 15:59






  • 1




    Here is another link, with a detailed answer of how to think in angular, when coming from a jquery background. It also states "do not augment angularjs with jquery" stackoverflow.com/a/15012542/1203811
    – KjetilNordin
    Nov 20 at 16:07










  • So the best practice is do not use jquery in Angular6, translate the same code to their own framework as i understood?.
    – CitizenG1
    Nov 20 at 16:19















up vote
1
down vote

favorite












I'm starting with angular 6 and I'm a bit confuse about how I can call a jquery function.



This is my app.conponent.html:



<div id="mySidenav" class="sidenav">
<a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
<a href="#">About</a>
<a href="#">Services</a>
<a href="#">Clients</a>
<a href="#">Contact</a>
</div>
<div class="main">
<nav class="navigator">
<ul class="nav nav-pills">
<li class="nav-item">
<a class="nav-link" onclick="openNav()" href="#"><i class="material-icons">menu</i></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Active</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
</ul>
</nav>
</div>
<router-outlet></router-outlet>


An this is my app.component.ts:



import { Component, OnInit  } from '@angular/core';
import * as $ from 'jquery';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
title = 'Look jQuery Animation working in action!';

public ngOnInit()
{
$(document).ready(function(){
$('#mySidenav').click(function(){
document.getElementById("mySidenav").style.width = "250px";
document.getElementById("main").style.marginLeft = "250px";
});
$('#mySidenav').click(function(){
document.getElementById("mySidenav").style.width = "250px";
document.getElementById("main").style.marginLeft = "250px";
});
});
}
}


If i use the tag $('div').click(function(){ and only one function, this works as expected. But I guess, I need to distinct each different id from its respective function.



What is the best practice to call a jQuery function from angular?



Thanks for your time.



Kind regards.










share|improve this question
























  • It not a really good pratice to use jQuery with angular. Take a look at that post : stackoverflow.com/questions/34707577/…
    – veben
    Nov 20 at 15:56










  • angularjs and angulario have their own patterns for binding event handlers to elements. You don't want to use jQuery for these operations.
    – Taplar
    Nov 20 at 15:59






  • 1




    Here is another link, with a detailed answer of how to think in angular, when coming from a jquery background. It also states "do not augment angularjs with jquery" stackoverflow.com/a/15012542/1203811
    – KjetilNordin
    Nov 20 at 16:07










  • So the best practice is do not use jquery in Angular6, translate the same code to their own framework as i understood?.
    – CitizenG1
    Nov 20 at 16:19













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I'm starting with angular 6 and I'm a bit confuse about how I can call a jquery function.



This is my app.conponent.html:



<div id="mySidenav" class="sidenav">
<a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
<a href="#">About</a>
<a href="#">Services</a>
<a href="#">Clients</a>
<a href="#">Contact</a>
</div>
<div class="main">
<nav class="navigator">
<ul class="nav nav-pills">
<li class="nav-item">
<a class="nav-link" onclick="openNav()" href="#"><i class="material-icons">menu</i></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Active</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
</ul>
</nav>
</div>
<router-outlet></router-outlet>


An this is my app.component.ts:



import { Component, OnInit  } from '@angular/core';
import * as $ from 'jquery';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
title = 'Look jQuery Animation working in action!';

public ngOnInit()
{
$(document).ready(function(){
$('#mySidenav').click(function(){
document.getElementById("mySidenav").style.width = "250px";
document.getElementById("main").style.marginLeft = "250px";
});
$('#mySidenav').click(function(){
document.getElementById("mySidenav").style.width = "250px";
document.getElementById("main").style.marginLeft = "250px";
});
});
}
}


If i use the tag $('div').click(function(){ and only one function, this works as expected. But I guess, I need to distinct each different id from its respective function.



What is the best practice to call a jQuery function from angular?



Thanks for your time.



Kind regards.










share|improve this question















I'm starting with angular 6 and I'm a bit confuse about how I can call a jquery function.



This is my app.conponent.html:



<div id="mySidenav" class="sidenav">
<a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
<a href="#">About</a>
<a href="#">Services</a>
<a href="#">Clients</a>
<a href="#">Contact</a>
</div>
<div class="main">
<nav class="navigator">
<ul class="nav nav-pills">
<li class="nav-item">
<a class="nav-link" onclick="openNav()" href="#"><i class="material-icons">menu</i></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Active</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
</ul>
</nav>
</div>
<router-outlet></router-outlet>


An this is my app.component.ts:



import { Component, OnInit  } from '@angular/core';
import * as $ from 'jquery';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
title = 'Look jQuery Animation working in action!';

public ngOnInit()
{
$(document).ready(function(){
$('#mySidenav').click(function(){
document.getElementById("mySidenav").style.width = "250px";
document.getElementById("main").style.marginLeft = "250px";
});
$('#mySidenav').click(function(){
document.getElementById("mySidenav").style.width = "250px";
document.getElementById("main").style.marginLeft = "250px";
});
});
}
}


If i use the tag $('div').click(function(){ and only one function, this works as expected. But I guess, I need to distinct each different id from its respective function.



What is the best practice to call a jQuery function from angular?



Thanks for your time.



Kind regards.







jquery angular angular6






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 19:39









veben

9532621




9532621










asked Nov 20 at 15:52









CitizenG1

306




306












  • It not a really good pratice to use jQuery with angular. Take a look at that post : stackoverflow.com/questions/34707577/…
    – veben
    Nov 20 at 15:56










  • angularjs and angulario have their own patterns for binding event handlers to elements. You don't want to use jQuery for these operations.
    – Taplar
    Nov 20 at 15:59






  • 1




    Here is another link, with a detailed answer of how to think in angular, when coming from a jquery background. It also states "do not augment angularjs with jquery" stackoverflow.com/a/15012542/1203811
    – KjetilNordin
    Nov 20 at 16:07










  • So the best practice is do not use jquery in Angular6, translate the same code to their own framework as i understood?.
    – CitizenG1
    Nov 20 at 16:19


















  • It not a really good pratice to use jQuery with angular. Take a look at that post : stackoverflow.com/questions/34707577/…
    – veben
    Nov 20 at 15:56










  • angularjs and angulario have their own patterns for binding event handlers to elements. You don't want to use jQuery for these operations.
    – Taplar
    Nov 20 at 15:59






  • 1




    Here is another link, with a detailed answer of how to think in angular, when coming from a jquery background. It also states "do not augment angularjs with jquery" stackoverflow.com/a/15012542/1203811
    – KjetilNordin
    Nov 20 at 16:07










  • So the best practice is do not use jquery in Angular6, translate the same code to their own framework as i understood?.
    – CitizenG1
    Nov 20 at 16:19
















It not a really good pratice to use jQuery with angular. Take a look at that post : stackoverflow.com/questions/34707577/…
– veben
Nov 20 at 15:56




It not a really good pratice to use jQuery with angular. Take a look at that post : stackoverflow.com/questions/34707577/…
– veben
Nov 20 at 15:56












angularjs and angulario have their own patterns for binding event handlers to elements. You don't want to use jQuery for these operations.
– Taplar
Nov 20 at 15:59




angularjs and angulario have their own patterns for binding event handlers to elements. You don't want to use jQuery for these operations.
– Taplar
Nov 20 at 15:59




1




1




Here is another link, with a detailed answer of how to think in angular, when coming from a jquery background. It also states "do not augment angularjs with jquery" stackoverflow.com/a/15012542/1203811
– KjetilNordin
Nov 20 at 16:07




Here is another link, with a detailed answer of how to think in angular, when coming from a jquery background. It also states "do not augment angularjs with jquery" stackoverflow.com/a/15012542/1203811
– KjetilNordin
Nov 20 at 16:07












So the best practice is do not use jquery in Angular6, translate the same code to their own framework as i understood?.
– CitizenG1
Nov 20 at 16:19




So the best practice is do not use jquery in Angular6, translate the same code to their own framework as i understood?.
– CitizenG1
Nov 20 at 16:19












1 Answer
1






active

oldest

votes

















up vote
0
down vote













just type in console:



npm i @types/jquery



and that's it.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53396767%2fhow-to-call-jquery-functions-from-angular%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    just type in console:



    npm i @types/jquery



    and that's it.






    share|improve this answer

























      up vote
      0
      down vote













      just type in console:



      npm i @types/jquery



      and that's it.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        just type in console:



        npm i @types/jquery



        and that's it.






        share|improve this answer












        just type in console:



        npm i @types/jquery



        and that's it.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 at 2:44









        Julio Rodriguez

        978




        978






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53396767%2fhow-to-call-jquery-functions-from-angular%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'