Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the alternative for jquery each in angularjs?

I want to remove jquery from my app.. But I want substitute for $.each in angularjs... How can I loop around dom elements ?? Here I have added my code.. I want to convert it into angularjs from jquery

app.directive('activeu', function($location) {         return function ($scope, element, attrs) {             var menuMain = $scope.$parent.menuMain;             var fullpath = $location.path().split('/');             var current = fullpath[2];              setTimeout(function() {                 $(".nav li a").each(function() {                     if ($(this).attr('href') ==  '#!/page/' + current) {                         $(this).parent().addClass('active');                         $(this).closest('li.root-li').addClass('active');                          if ($(this).closest('li.parent').length > 0) {                             $(this).closest('li.parent').addClass('active');                         }                     }                 });                  $(".nav li a").on('click', function() {                     current = $(this).attr('href');                     $("#pages-menu .navigation li").each(function() {                         if ($(this).hasClass('active')) {                             $(this).removeClass('active');                         }                     });                      $(".nav li a").each(function() {                         if ($(this).attr('href') ==  current) {                             $(this).parent().addClass('active');                             $(this).closest('li.root-li').addClass('active');                              if ($(this).closest('li.parent').length > 0) {                                 $(this).closest('li.parent').addClass('active');                             }                         }                     });                 });             }, 500);         };     }); 
like image 390
Amb Avatar asked Jul 09 '13 05:07

Amb


People also ask

Can we use jQuery in AngularJS?

Does AngularJS use the jQuery library? Yes, AngularJS can use jQuery if it's present in your app when the application is being bootstrapped. If jQuery is not present in your script path, AngularJS falls back to its own implementation of the subset of jQuery that we call jQLite.

Is Angular a replacement for jQuery?

Angular Vs jQuery: Key Differences Between Angular And JQuery. It is used for creating single-page applications. jQuery is a Javascript-based library that is primarily used for DOM manipulation whereas Angular is a front-end development framework that is used for creating single-page applications.

Which is best jQuery or AngularJS?

It is well understood that JQuery is best suited for DOM manipulation and Angular JS is best suited for Web application development. Angular JS is used to develop robust applications and to add more functionality or to perform DOM manipulation on the website we can use JQuery.

Is jQuery necessary AngularJS?

This is to minimize dependencies. Yet, if you load jQuery before angular, then angular will use jQuery. Most of the time, you do not need to use jQuery. Even so much that, for beginners, it is advised to leave out jQuery completely as there would be a tendency to use jQuery when there is an easy / angular way.


2 Answers

angular.forEach(angular.element("li a"), function(value, key){      var a = angular.element(value);      a.addClass('ss'); }); 

You can convert it into object and then, you can use it..

like image 128
Amb Avatar answered Sep 30 '22 00:09

Amb


you can use the loop statement angular.forEach()

var values = {name: 'misko', gender: 'male'}; angular.forEach(values, function(value, key){   console.log(key + ': ' + value); }); 
like image 43
Arun P Johny Avatar answered Sep 29 '22 22:09

Arun P Johny