码迷,mamicode.com
首页 > 其他好文 > 详细

怎么在ng-repeat生成的元素上操作dom

时间:2016-11-19 07:45:22      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:数据   items   ems   操作   操作dom   exec   repeat   class   元素   

这个问题其实对初学者来说,都 有这样的经历,用ng-repeat生成的元素用js怎么也获取不到;这个其中原由是:angular拥有自动化渲染DOM的特性,它能帮助我们专注于操作数据,而页面的渲染则由angular自身来完成。这就造成了 ng-repeat 循环完成后angular并不会告诉我们dom渲染完了;换句话说就是不知道angular渲染dom何时完成(得不到生成的元素是因为这个元素还没有渲染出来)。

也就是说要找一个方法使你知道什么时候angular渲染dom完成了,你再才来操作dom,得到你想要的元素、进行操作。。。。

<div ng-repeat="item in items" on-finish="ngRepeatFinished">
    <div>{{item.name}}}<div></div>
//js/////
var module = angular.module(;fang‘, [])
    .directive(‘onFinish‘, function ($timeout) {
    return {
        restrict: ‘A‘,
        link: function (scope, element, attr) {
            if (scope.$last === true) {
                $timeout(function () {
                    scope.$emit(‘ngRepeatFinished‘);
                });
            }
        }
    }});

$scope.$on(‘ngRepeatFinished‘, function(ngRepeatFinishedEvent) {
    //you also get the actual event object
    //do stuff, execute functions -- whatever...});


这样你就可以用js找到ng-repeat生成的元素了;并进行相关元素操作。

怎么在ng-repeat生成的元素上操作dom

标签:数据   items   ems   操作   操作dom   exec   repeat   class   元素   

原文地址:http://www.cnblogs.com/flxy-1028/p/6079698.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!