我想显示一个动态选择,反应组件中的值介于1和100之间.
在Angular 2中
<select> <option *ngFor="let i of numbers">{{i}}</option> </select> <!--numbers is an array [1,2,3,4,...100]-->
React怎么样?
您可以在数字上使用地图并动态创建选项,如下所示:
<select> { numbers.map(el => <option value={el} key={el}> {el} </option>) } </select>
检查此示例:
var numbers = [...Array(100).keys()]; var App = () => { return( <select> { numbers.map(el => <option value={el} key={el}> {el} </option>) } </select> ) } ReactDOM.render(<App/>,document.getElementById('app'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id='app'/>