ng-repeat后运行的指令

时间:2014-07-01 14:49:01

标签: javascript jquery angularjs angularjs-directive

我对这个Calling a function when ng-repeat has finished有类似的问题,但我有指令和ng-repeat内部。

<my-dropdown>
  <li ng-repeat="item in items"><a>{{item.text}}</a></li>
</my-dropdown>

在我的下拉列表中,我在两个地方进行了转换(一个用于列表,一个用于标题),我需要将类ng-hide添加到除使用jQuery之外的所有项目。所以我需要在ng-repeat之后运行代码。我试图将我的指令中的优先级设置为2000或0(ngRepeat有1000),但这不起作用。当我运行element.find('li');

时,我有一个项目
return {
    restrict: 'E',
    require: '?ngModel',
    template: ['<div class="btn-group dropdown-button">',
               '  <div class="btn caption" ng-transclude></div>',
               '  <button class="btn dropdown-toggle" data-toggle="dropdown">',
               '    <span class="caret"></span>',
               '  </button>',
               '  <ul class="dropdown-menu" ng-transclude></ul>',
               '</div>'].join('\x0D'), // newline - peach replace newlines, gods only know why
    transclude: true,
    replace: true,
    compile: function(element, attrs, transclude) {
        // I've used compile because I wante to test the transclude function

        return function(scope, element, attrs, ngModelCtrl) {
            element.find('.caption li').attr('ng-hide', 'true');
            var selected_index = 0;
            function setValue(item) {
                var value = item.attr('value');
                ngModelCtrl.$setViewValue(value ? $interpolate(value)(scope) : item.index());
            }
            var caption = element.find('.caption');
            function update() {
                // for model with ng-repeat it return 1 item
                console.log(attrs.ngModel + ' ' + caption.find('li').length);
                caption.find('li').removeClass('ng-hide').not(':eq(' + selected_index + ')').addClass('ng-hide');
            }
            if (ngModelCtrl) {
                element.on('click', 'ul li', function() {
                    var self = $(this);
                    selected_index = self.index();
                    scope.$apply(function() {
                        setValue(self);
                    });
                    var selected = self.attr('selected');
                    if (selected) {
                        scope.$eval(selected);
                    }
                });

                if (!ngModelCtrl.$viewValue && attrs.placeholder) {
                    $('<li>' + attrs.placeholder + '</li>').appendTo(caption);
                    selected_index = caption.find('li').length-1;
                } else {
                    selected_index = ngModelCtrl.$viewValue || 0;
                }
                setValue(element.find('ul li:eq(' + selected_index + ')'));

                ngModelCtrl.$viewChangeListeners.push(function() {
                    scope.$eval(attrs.ngChange);
                    update();
                });
                ngModelCtrl.$render = function() {
                    if (!ngModelCtrl.$modelValue) {
                        selected_index = 0;
                        update();
                    } else {
                        $(element).find('ul li').each(function(i) {
                            var self = $(this);
                            var value = self.attr('value');
                            if (value && ngModelCtrl.$modelValue == $interpolate(value)(scope) ||
                                ngModelCtrl.$modelValue == i) {
                                selected_index = i;
                                update();
                                return false;
                            }
                        });
                    }
                };
            }
            update();
        };
    }
}

0 个答案:

没有答案