我正在使用AngularJS将div添加到节中,并且我希望div具有静态起始宽度并在添加更多div时动态增长,该怎么做?
该代码不起作用:
<body ng-app="plunker" ng-controller="MainCtrl">
<section>
<div ng-repeat="div in divs track by $index">
<div class="square"></div>
</div>
</section>
<button ng-click="add()">add</button>
</body>
section {
border: 1px solid red;
overflow: hidden;
padding: 10px 0;
width: 400px;
}
.square {
width: 100px;
height: 100px;
background: #000;
margin-right: 10px;
float: left;
margin-top: 1em;
}
柱塞链接:
http://plnkr.co/edit/SqGXBh9zXK2P3LCIVOPG?p=preview
最佳答案
您需要做的只是:
对于部分:
min-width: 450px; //min width of all blocks that inside your section
display: inline-block; //to make width "grow" with content
对于正方形:
display: inline-block; //instead of float, because float makes height to 0
http://plnkr.co/edit/azOKubY7b371u2Pt7JbD?p=preview
//注意:我将
square
类移到了ng-repeat
的父节点,但这不是必须的,如果要具有以前的结构,只需添加display: inline-block;
即可。