从 DOM 获取元素
get element from DOM
你好,我正在尝试从角度指令中的 DOM 获取元素。
此元素:
点击图片
我试图通过未知 id 获取此元素,这是代码:
传递用户对象:
<dashboard user="user" action="pageSelect(name,pageNumber)"></dashboard>
在模板网址指令中:
<section>
<li id="Dashboard{{$index}}" ng-repeat="dashboard in user.NavigationMenu">
<h3 class="PovDashboard">
<i class="fa fa-tachometer"></i>
{{dashboardName}}
</h3>
<ul class="povPages">
<li ng-repeat="page in dashboard.Pages"> <i class="povIconRight fa fa-angle-double-right"></i></li>
</ul>
</li>
这就是问题所在:
scope.$watch('user', function(newValue) {
if (newValue !== undefined) {
console.log(scope.user.NavigationMenu[0].Pages);
var defaultDashboard = scope.user.DefaultDashboardID;
console.log(scope.user);
angular.forEach(scope.user.NavigationMenu,function(value,key){
if(value.ID === defaultDashboard){
console.log(key);
// i tried everything
var s = '#Dashboard' + key;
var e = angular.element.find(s)
//e is empty
console.log(e);
//i'm trying to do
//e.('.povPages').first().css("display","block");
}
})
}
});
提前致谢
您没有使用 jqLite 包装器angular.element()
语法正确,我想您应该尝试一下:
angular.element(s).find('ul').css('display', 'block');
如果您不使用jQuery,那么.find()
将仅查找标签名称。
从文档中:
find(( - 仅限于按标记名称查找
或者最好用ng-show/ng-hide
的角度方式来做:
<ul class="povPages" data-ng-show='isVisible'>
立即初始化$scope.isVisible = false;
.$watch()
执行此操作:
scope.$watch('user', function(newValue) {
if (newValue != undefined) {
console.log(scope.user.NavigationMenu[0].Pages);
scope.isVisible = newValue != undefined ? true : false;
console.log(scope.user, scope.isVisible);
}
});
示例演示:
angular.module('demoapp', []).controller('demoCtrl', ['$scope', '$timeout',
function($scope, $timeout) {
$scope.arr = [{
text: "one"
}, {
text: "two"
}];
$scope.isVisible = false;
$timeout(function() {
$scope.isVisible = true;
}, 2000);
}
]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<div ng-app='demoapp' ng-controller='demoCtrl'>
<div ng-repeat="obj in arr">
<h1>see if list is visible == {{isVisible}} {{obj.text}}</h1>
<ul ng-show='isVisible'>
<li>should be visible after value change. {{obj.text}}</li>
</ul>
</div>
</div>
相关文章:
- React - 从 DOM 元素获取组件以进行调试
- 如何使用 JQuery 从相对元素获取 html 文本
- 从同一域上的 iframe 中的元素获取值
- 使用本机 Javascript 从 DOM 元素获取选择器字符串
- 使用jquery从另一个元素获取使用id的输入的id
- 如何从角度元素获取编译的节点值
- 如何为不同的元素获取不同的父类
- 从 jQuery 中的子元素获取父 tr 元素
- 当浮点部分为空时,从 JQuery 中的输入元素获取数字值不起作用
- 从 XML 元素获取文本
- 从音频元素获取音频样本
- 从所选元素 html 上方的元素获取属性值
- 使用 jQuery.data 从 dom 元素获取布尔值
- 如何通过jquery从某个元素获取部分文本
- 从输入元素获取表单和表单数据对象
- 从动态元素获取挖空视图的大小
- 从中继器循环中显示的子自定义元素获取正确的上下文.奥蕾莉亚
- 如何使用 JavaScript 或 JQuery 从输入类型=文件 html 元素获取文件名
- 从父元素获取 ID 并将它们放在数组中
- 尝试从选项元素获取 HTML