2013-07-09 33 views
25

Tôi muốn loại bỏ jquery khỏi ứng dụng của mình .. Nhưng tôi muốn thay thế cho $ .each trong angularjs ... Làm thế nào tôi có thể vòng quanh các phần tử dom ?? Ở đây tôi đã thêm mã của tôi .. Tôi muốn chuyển đổi nó thành angularjs từ jquerySự thay thế cho jquery mỗi trong angularjs là gì?

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

Trả lời

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

Bạn có thể chuyển đổi nó vào đối tượng và sau đó , bạn có thể sử dụng nó ..

13

bạn có thể sử dụng câu lệnh lặp angular.forEach()

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

nó mang lại cho tôi như var htm = "Login" .. Tôi có thể truy cập nó như htm.href .. nhưng thay vào đó nếu tôi muốn truy cập nó như htm.attr ('href'), làm sao tôi có thể làm được không ?? – Amb

+0

@amss Tôi nghĩ bạn cần chia sẻ một số chi tiết hơn về loại dữ liệu bạn đang cố gắng lặp lại và đầu ra mong muốn là gì –

+0

Tôi đã chỉnh sửa câu hỏi của mình ... Tôi muốn thay thế mỗi câu hỏi thành angular.forEach – Amb

Các vấn đề liên quan