在angularjs中,jquery的替代方法是什么?

Amb*_*Amb 26 jquery angularjs

我想从我的应用程序中删除jquery ..但我想在angularjs中替换$ .each ...我怎么能循环dom元素?在这里,我添加了我的代码..我想将它从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);
        };
    });
Run Code Online (Sandbox Code Playgroud)

Amb*_*Amb 66

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

你可以把它转换成对象然后,你可以使用它..


Aru*_*hny 14

你可以使用循环语句angular.forEach()

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