0

I want to update the width of my UL based on the number of LI elements inside that UL, currently I'm doing this by creating a custom directive with a $timeout which is actually delaying the page to be loaded with the content and also I achieved this with a mixture of jQuery and AngularJS.

Is there a better way to do this in pure angular ways by watching for a variable in the controller and passing the same to ng-style? My page contains several ULs with variable number of LIs. Please suggest.

app.directive('setUlLength', function ($timeout) {
    return {
        restrict: 'A',
        link: function (scope, element, attr) {
            $timeout(function () {
                var totli = $(element).find('ul').children('li').length;
                var liwid = $(element).find(".prod-gall ul li:first").outerWidth(true);
                var setUlWidth = totli * liwid;
                $(element).find('.prod-gall ul').css({ 'width': setUlWidth + 'px' });
            });
        }
    };
});
Ajay Srikanth
  • 1,095
  • 4
  • 22
  • 43
  • Wouldn't it be a best idea to do that in CSS? – Blackhole Jan 13 '15 at 21:49
  • Do you know any reference? I doubt if we can calculate width of the 'li' element in css? – Ajay Srikanth Jan 13 '15 at 21:53
  • It was a suggestion :) . The probability was high that your `
  • ` was all on the same horizontal line, and that you just wanted to adapt the size of the container. In such a case, CSS is enough. But of course, it's impossible to calculate the sum of the widths of the children in CSS.
  • – Blackhole Jan 13 '15 at 21:59
  • As long as the sum of widths of all my
  • are with in the defined width of my
      they stay in the same horizontal line, when the sum of widths of
    • goes beyond the
        's defined width they wrap to the next line, this what I want to fix by setting the
          widht once all the
        • are loaded.
  • – Ajay Srikanth Jan 13 '15 at 22:27
  • Sounds like you don't use `min-width`. – Blackhole Jan 13 '15 at 22:39
  • I use width here because, if I use min-width even if the width of sum of all
  • is less than the min-width my
      width remains as my min-width, which I don't want.
  • – Ajay Srikanth Jan 13 '15 at 23:25