Javascript Angular无法按ng repeat div内的类获取元素
我想通过JQuery获取他的类的一个元素,但由于某些原因,这不起作用。JQuery对象的长度为0,只有当我希望元素中的类具有Javascript Angular无法按ng repeat div内的类获取元素,javascript,jquery,angularjs,Javascript,Jquery,Angularjs,我想通过JQuery获取他的类的一个元素,但由于某些原因,这不起作用。JQuery对象的长度为0,只有当我希望元素中的类具有ng repeat时,才会发生这种情况 index.html plunker:实现这一点的一种方法是将查询包装在$timeout中,这将导致查询在下一个摘要循环上运行: .directive('user', function($timeout) { return function(scope, ele) { console.log('Im he
ng repeat
时,才会发生这种情况
index.html
plunker:实现这一点的一种方法是将查询包装在
$timeout
中,这将导致查询在下一个摘要循环上运行:
.directive('user', function($timeout) {
return function(scope, ele) {
console.log('Im here');
$timeout(function () {
console.log($('.user'));
});
}
})
使用超时
.directive('user', function($timeout) {
return function(scope, ele) {
console.log('Im here');
$timeout(function() {
console.log(ele[0].offsetHeight);
},0);// this is not working the length is 0
}
})
返回的长度为0,因为div尚未呈现到DOM上<代码>$timeout是一种解决方法,但我会使用:
angular.element(document).ready(function () {
console.log($('.user').length)
});
您的指令标记是基本的。您应该使用如下所示的指令,并且不必使用$timeout hack。link()函数用于在加载DOM后对其进行操作
angular.module('project').directive('user', [
function () {
return {
templateUrl: 'views/DirectiveSample.html',
restrict: 'A',
scope: {
users: '='
},
link: function postLink(scope, element, attrs) {
console.log($('.user'));
}
};
}
]);
你想对元素做什么,因为“角度方式”是使用角度而不是jQuery来操作元素?我需要计算他的宽度和高度
console.log(ele)
这是您的指令元素。您希望看到这一点吗?但为什么这只发生在ng repeat div中?在链接块中使用$last要好得多
angular.element(document).ready(function () {
console.log($('.user').length)
});
angular.module('project').directive('user', [
function () {
return {
templateUrl: 'views/DirectiveSample.html',
restrict: 'A',
scope: {
users: '='
},
link: function postLink(scope, element, attrs) {
console.log($('.user'));
}
};
}
]);