2013-07-09 83 views
25

我想從我的應用程序中刪除jquery ..但我想在angularjs中替換$ .each ... 如何循環dom元素? 在這裏,我已經加了我的代碼..我想將其轉換成angularjs從jQuery的jQuery的每個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); 
     }; 
    }); 

回答

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

你可以把它轉換成對象,然後,你可以使用它..

13

可以使用循環語句angular.forEach()

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

其給我像var htm =「Login」..我可以像訪問它htm.href ..但相反,如果我想訪問它像htm.attr('href'),怎麼樣 我可以做嗎?? – Amb

+0

@amss我想你需要分享一些更多的細節你試圖迭代什麼樣的數據和什麼是所需的輸出 –

+0

我編輯我的問題...我想用jquery替換angular.forEach – Amb

相關問題