Skip to content
Related Articles

Related Articles

AngularJS ng-jq Directive

Improve Article
Save Article
  • Difficulty Level : Expert
  • Last Updated : 26 Aug, 2022
Improve Article
Save Article

The ng-Jq directive in AngularJS allows forcing the library used by angular.element library. The forcing of jQLite should happen when we leave ng-jq blank, otherwise set the name of the jquery variable under the window (e.g., jQuery). jQLite is directly built into AngularJS and is an important subset of jQuery. By default, AngularJS use jQLite. This directive is looked upon by the AngularJS when it needs to get loaded, and it does not wait for the DOMContentLoaded event at all. It should get placed on the element which comes up before going to the script which is responsible for leading the angular. Aside from the ng-app directive, if you add the ng-jq directive, you can specify the jQuery name which will be available under the window, which is going to be extremely crucial when you are going to use jQuery with an alias variable.

Note: Only the first instance of the ng-jq directive is going to be used by the AngularJS, while all the others will get ignored. Use jQuery to load the jQuery library before loading the AngularJS then angular will skip jQLite and it will start to use the jQuery library.

Syntax: The ng-jq Directive can be used:

As an element:

<ng-jq [ng-jq="string"]>
    Content...
</ng-jq>

As an attribute:

<element [ng-jq="string"]>
    Content...
</element>

Parameter value: It contains a single parameter ng-jq which is optional. The name of the library must be specified under the window to use for the angular.element. 

Example: This example describes the use of the ng-jq Directive in AngularJS, by checking the existence of jQuery.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <title>AngularJS ng-jq Directive</title>
    <script data-require="jquery@2.1.3" data-semver="2.1.3" 
    </script>
  
    <script>
        var jQuery_2_1_3 = $.noConflict(true);
    </script>
  
    <script data-require="angular.js@1.4.0-rc.0" 
        data-semver="1.4.0-rc.0"
    </script>
  
    <script>
        angular.module('ngAppTest', [])
            .controller('MainCtrl', function () {
                this.isUsingJQuery =
                    angular.element.toString()
                    .indexOf('jQuery') !== -1;
            })
    </script>
</head>
  
<body style="text-align:center;" ng-app="ngAppTest" 
    ng-jq="jQuery_2_1_3" ng-controller="MainCtrl as vm">
  
    <h1 style="color:green">GeeksforGeeks</h1>
    <h3 style="color:purple">ng-jq Directive</h3>
    <p>is using jQuery : {{ vm.isUsingJQuery }}</p>
</body>
  
</html>

Output:

 


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!