2013-07-09 6 views
25

Voglio rimuovere jquery dalla mia app .. Ma voglio sostituire $ .each in angularjs ... Come posso aggirare dom elementi ?? Qui ho aggiunto il mio codice .. voglio convertirlo in angularjs da jQueryQual è l'alternativa per jquery in angularjs?

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); 
     }; 
    }); 

risposta

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

È possibile convertire in oggetto e poi , puoi usarlo ..

13

è possibile utilizzare l'istruzione ciclica angular.forEach()

var values = {name: 'misko', gender: 'male'}; 
angular.forEach(values, function(value, key){ 
    console.log(key + ': ' + value); 
}); 
+0

mi dà come var htm = "Login" .. Posso accedervi come htm.href .. ma invece se voglio accedervi come htm.attr ('href'), Come posso farlo?? – Amb

+0

@amss Penso che tu abbia bisogno di condividere qualche dettaglio in più che tipo di dati stai provando ad iterare e qual è l'output desiderato –

+0

Ho modificato la mia domanda ... Voglio sostituire jquery ciascuno con angular.forEach – Amb

Problemi correlati