简体   繁体   English

AngularJS ng-repeat溢出

[英]AngularJS ng-repeat overflow

I'm trying to output html anchors inside a fixed width container. 我正在尝试在固定宽度的容器内输出html锚。 When using angular's ng-repeat the links overflow outside of the container. 使用angular的ng-repeat时,链接会在容器外部溢出。 Below is the code snippet for ng-repeat. 以下是ng-repeat的代码段。 Refer to the jsfiddle for an example of the overflow. 有关溢出的示例,请参考jsfiddle。

http://jsfiddle.net/n1Lkybwf/2/ 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>

The problem is that the width of the container is being calculated before the DOM is rendered (DOM rendering is asynchronous). 问题在于容器的宽度是在渲染DOM之前计算的(DOM渲染是异步的)。 In Angular, This is the order things are happening... 在Angular中,这是事物发生的顺序...

By default, anchor tag displays as inline-block while a simple div as a block. 默认情况下,锚标记显示为行内块,而简单div显示为块。 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. 这就是为什么解决方案是ng- div重复div而不是anchor标签,然后通过使用display:inline-block设置div的样式来修复块显示。

  <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/ 这是一个工作的小提琴: http : //jsfiddle.net/9zhc3bqu/

You can add a div and put the ng-repeat on the div, as shown below 您可以添加一个div并将ng-repeat放在div上,如下所示

<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>

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. 或者<br/>标记也可以。

<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>

you have fix width (200px) that's why the container dosen't expand with the elements inside. 您具有固定宽度(200px),这就是为什么容器不随内部元素一起扩展的原因。

you should remove the width, and change the display if you want the container to expand with the elements. 如果希望容器随元素一起扩展,则应删除宽度并更改显示。

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

or as Bhaskor Jyoti Sarmah suggested overflow:scroll; 或如Bhaskor Jyoti Sarmah所建议的overflow:scroll;

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM