jQuery is a well written JavaScript code that provides most frequently used functionality in the web pages with best possible ways. Its concise, fast, cross browser compatible and increases developer productivity by providing short and sweet functions.
Most frequently used jQuery functionality can be achieved using AngularJS however there are many functionalities of jQuery that can't be achieved using AngularJS, in those scenarios, we will need jQuery in AngularJS.
AngularJS by default comes with jqLite that works without the reference of jQuery file in the web page, using that we can call most frequently used jQuery functions of jQuery. jqLite doesn't support selectors.
Let's see an example of how to use most of jQuery funtions (provided in AngularJS jqLite) in AngularJS.
In below code snippet, we have a button, clicking on button calls AngularJS ChangeContent() function. This function tries to find out "p1" element using angular.element (this delegates to AngularJS built-in subset of jQuery) and using plain JavaScript function document.getElementById
and then calls the .text
function of jqLite by passing the parameter value to set into "p1" element.
<script>
var app = angular.module('app', [])
.controller("jqLiteController", function ($scope) {
$scope.ChangeContent = function () {
// call jQuery functiona using angular.element
angular.element(document.getElementById("p1")).text("Value changed");
};
});
</script>
<div ng-app="app" ng-controller="jqLiteController">
<button ng-click="ChangeContent()">Toggle</button>
<p id="p1" style="padding:5px;border:1px solid #c0c0c0;">Show/Hide</p>
</div>
Please note that as jqLite doesn't support jQuery selectors so we are using document.getElementById function on top of angular.element.
Currently, jqLite supports following 35 functions of jQuery
Source: AngularJS official website
Views: 23869 | Post Order: 66