Angular 2 : Binding dropdowns values with 'n' numbers

标签 angular typescript

使用 Angular2 循环将下拉值与 1 到 100 之间的数字绑定(bind)的最佳方法是什么?

对于有限数量的值,我使用 Ngprime 下拉列表,但我如何实现 'n' 个值?

模板:

<p-dropdown [options]="tests" [(ngModel)]="selectedCar" [style]="{'width':'150px'}" editable="editable" placeholder="Select a Brand"></p-dropdown>

组件:

this.tests = [];
this.test.push({label: 'Audi', value: 'Audi'});

谁能指导我?

最佳答案

在 Angular 2 中,带有从 1 到 100 选项的下拉列表将是这样的:

在组件中:

export class DropDownClass {
  constructor() {
    this.numbers = new Array(100).fill(0).map((x,i + 1)=>i); // [1,2,3,4,...,100]
  }
}

在模板中:

<select name="my-dropdown" [(ngModel)]="myDropdownModel">
    <option *ngFor="let number of numbers" [value]="number">{{number}}</option>
</select>

关于 Angular 2 : Binding dropdowns values with 'n' numbers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43866260/

相关文章:

asp.net - 在 asp.net core 2.0 中使用 angular 5 上传文件。文件为空

angular - 以 Angular 动态更新 Monaco Editor 主题

reactjs - 将 TypeScript 与 React、无状态组件结合使用

generics - TypeScript 泛型

javascript - 如何从函数返回的对象键和值生成字符串文字类型?

angular - 让 angular-oauth2-oidc 从其他选项卡检索访问 token

javascript - Angular2 和 Ng2Bootstrap 问题 : ComponentLoaderFactory

返回旧步骤时,Angular Material Stepper 会导致 mat-form 字段验证动态表单

angular - 错误 : Schema validation failed with the following errors: Data path "" should NOT have additional properties(project)

angular - 如何修复 'TypeError: this.productService.getProducts is not a function' 错误?