2

I'm trying to output html anchors inside a fixed width container. When using angular's ng-repeat the links overflow outside of the container. Below is the code snippet for ng-repeat. Refer to the jsfiddle for an example of the overflow.

http://jsfiddle.net/n1Lkybwf/2/

<div style="width: 200px; padding: 5px; border: solid 3px #000;">
    <a ng-repeat="tag in tags" style="margin-right: 5px;">{{tag}}</a>
</div>
4
  • Try adding display:inline-block to the links. Commented Nov 12, 2014 at 6:24
  • I've added this to the fiddle. It does not seem to fix the issue. Commented Nov 12, 2014 at 6:26
  • 1
    I don't see it in there? Check jsfiddle.net/s3f9re29/1 Commented Nov 12, 2014 at 6:31
  • Yeah I forgot to save the fiddle when I put it in there. I also added it to the wrong element. Your answer is the cleanest. Thanks! Commented Nov 12, 2014 at 6:39

5 Answers 5

4

The problem is that the width of the container is being calculated before the DOM is rendered (DOM rendering is asynchronous). In Angular, This is the order things are happening...

By default, anchor tag displays as inline-block while a simple div as a block. That's why the solution is to ng-repeat the div not the anchor tag, then to fix the block-displaying by styling the div with display: inline-block.

  <div class="tag" ng-repeat="tag in tags" style="display:inline-block;">
    <a  style="margin-right: 5px;">{{tag}}</a>
  </div>

Here is a working fiddle : http://jsfiddle.net/9zhc3bqu/

Sign up to request clarification or add additional context in comments.

1 Comment

It does appear that using display: inline-block can just be applied to the anchor tag without having to wrap it in a div however.
1

You can add a div and put the ng-repeat on the div, as shown below

<div ng-controller="MyCtrl">
    <div style="width: 200px; padding: 5px; border: solid 3px #000;">
        <div ng-repeat="tag in tags">
        <a  style="margin-right: 5px;">{{tag}}</a>
        </div>
    </div>

     <div style="width: 200px; padding: 5px; border: solid 3px #000; margin-top: 10px;">
         <a style="margin-right: 5px;">sampletag1</a>
         <a style="margin-right: 5px;">sampletag2</a>
         <a style="margin-right: 5px;">sampletag3</a>
         <a style="margin-right: 5px;">sampletag4</a>
    </div>
</div>

2 Comments

This would put each anchor on its own line. I would like the links to be inline and wrap automatically, just like the second div.
That does seem to produce the intended result. Interesting that the html that angular injects doesn't adhere to the containing object. Is this a common occurrence with html that is injected to the DOM? The html that angular outputs is essentially the same as the html that I hardcoded in the fiddle.
0

May be you can use the scroll bar to hide the overflow part

<div ng-controller="MyCtrl">
    <div style="overflow-y : scroll;width: 200px; padding: 5px; border: solid 3px #000;">
        <a ng-repeat="tag in tags" style="margin-right: 5px;">{{tag}}</a>
    </div>

     <div style="width: 200px; padding: 5px; border: solid 3px #000; margin-top: 10px;">
         <a style="margin-right: 5px;">sampletag1</a>
         <a style="margin-right: 5px;">sampletag2</a>
         <a style="margin-right: 5px;">sampletag3</a>
         <a style="margin-right: 5px;">sampletag4</a>
    </div>
</div>

Or a <br/> tag might do.

<div ng-controller="MyCtrl">
    <div style="overflow-y : scroll;width: 200px; padding: 5px; border: solid 3px #000;">
        <a ng-repeat="tag in tags" style="margin-right: 5px;">{{tag}}<br/></a>
    </div>

     <div style="width: 200px; padding: 5px; border: solid 3px #000; margin-top: 10px;">
         <a style="margin-right: 5px;">sampletag1</a>
         <a style="margin-right: 5px;">sampletag2</a>
         <a style="margin-right: 5px;">sampletag3</a>
         <a style="margin-right: 5px;">sampletag4</a>
    </div>
</div>

1 Comment

I do not wish to have a scrollbar as a fix. The second div in the fiddle is how the result should look. The links should wrap automatically.
0

you have fix width (200px) that's why the container dosen't expand with the elements inside.

you should remove the width, and change the display if you want the container to expand with the elements.

http://jsfiddle.net/cmbnyack/1/

or as Bhaskor Jyoti Sarmah suggested overflow:scroll;

1 Comment

I want the links to wrap inside the container. I don't want the container to change. If you view the fiddle you can see the desired result below the overflow.
0

Try this

<div ng-controller="MyCtrl">
    <div style="width: 200px; padding: 5px; border: solid 3px #000;">
        <a ng-repeat="tag in tags" style="margin-right: 5px;">
            <span ng-if="(tags_inx%2) != 0">
            {{tag}}
</span>
            <span ng-if="(tags_inx%2) == 0">
            {{tag}}<br/>
</span>
</a>
    </div>

     <div style="width: 200px; padding: 5px; border: solid 3px #000; margin-top: 10px;">
         <a style="margin-right: 5px;">sampletag1</a>
         <a style="margin-right: 5px;">sampletag2</a>
         <a style="margin-right: 5px;">sampletag3</a>
         <a style="margin-right: 5px;">sampletag4</a>
    </div>
</div>

1 Comment

Just put a space after the tag if (tags_inx%2) != 0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.