如何在angularjs指令中使用$(this)?

时间:2014-09-30 10:20:28

标签: angularjs performance

我在angularjs中写了一个指令。我使用以下代码..

$('#TableID tr:has(td)')

但我不想使用#TableID即表ID。出于这个原因,我如何使用$(this)或this或any think而不是上面的代码。 我的整个指令如下:

directives.directive('niGroup', ['$timeout', function ($timeout) {
        return {
            restrict: 'A',
            scope: false,
            link: function (scope, element, attrs) {
                function groupTable($rows, startIndex, total) {
                    if (total === 0) {
                        return;
                    }
                    var index, currentIndex = startIndex, count = 1, lst = [];
                    var tds =  $rows.find('td:eq(' + currentIndex + ')');
                     var ctrl = $(tds[0]);
                    //var $this = $(this);
                    //var ctrl = $this.text();
                    lst.push($rows[0]);
                    for (index = 1; index <= tds.length; index++) {
                        var t1 = ctrl.text();
                        var t2 = $(tds[index]).text();
                        if (ctrl.text() == $(tds[index]).text()) {
                            count++;
                            $(tds[index]).css("display", "none");//hide(); //addClass('deleted');
                            lst.push($rows[index]);
                        }
                        else {
                            if (count > 1) {
                                ctrl.attr('rowspan', count);
                                groupTable($(lst), startIndex + 1, total - 1);
                            }
                            count = 1;
                            lst = [];
                            ctrl = $(tds[index]);
                            lst.push($rows[index]);
                        }
                    }
                }
                scope.niGroups = scope.$eval(attrs.niGroup);
                //get ListName
                scope.listName = scope.niGroups.gridData;
                scope.from = 0;
                scope.groupLength = 1;
                if (scope.niGroups.groupBy != undefined && scope.niGroups.groupBy.length > 1) {
                    scope.from = parseInt(scope.niGroups.groupBy.split(',')[0]);
                    scope.groupLength = parseInt(scope.niGroups.groupBy.split(',')[1]);
                }
                scope.$watchCollection(
                  scope.listName,
                    function (newValue, oldValue) {
                        if (newValue !== oldValue) {
                            $timeout(function () {
                                groupTable($('tr:has(td)'), scope.from, scope.groupLength);
                            }, 0);
                        }
                    }
                );
            }
        };
    }]);

1 个答案:

答案 0 :(得分:0)

  

使用$ event获取事件对象,您可以从中获取   事件目标并与jquery一起使用。

看看here