angularjs 中每个 jquery 的替代方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17540745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the alternative for jquery each in angularjs?
提问by Amb
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
我想从我的应用程序中删除 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);
};
});
回答by Amb
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..
您可以将其转换为对象,然后,您可以使用它..
回答by Arun P Johny
you can use the loop statement angular.forEach()
您可以使用循环语句angular.forEach()
var values = {name: 'misko', gender: 'male'};
angular.forEach(values, function(value, key){
console.log(key + ': ' + value);
});