Angular 2设置选择的开始值 [英] Angular 2 Set begin value of select

查看:113
本文介绍了Angular 2设置选择的开始值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的选择代码:

   <select class="form-control" [(ngModel)]="selectedPrototypeSelector" (ngModelChange)="onPrototypeChange()">
       <option *ngFor="#p of prototypes" [value]="p.selector">
             {{ p.selectorName }}
       </option>
   </select>

如何为该 select选项设置标准开始值?我以为我必须设置此变量:

How do I set a standard begin value to this select option I thought i have to set this variable:

 selectedPrototypeSelector: string;

为此: selectedPrototypeSelector:string ="Test";

但是"Test" 不在选择框中首先显示.begin值为空,我如何将其设置为:选择您的选项.

But the "Test" is not showing first in the select box The begin value is empty, how do I set this for example to: Select your option.

这里是 PLUNKER

如您所见,当我将值设置为 p.selector 时,选择框为空;对于第二个选择,当我给出 constraint 的值时,选择框也为空:

as you can see the select box is empty while i set the value p.selector and the same for the second select where i give the value of constraint:

 <select class="form-control" [(ngModel)]="expression.constraint">
     <option *ngFor="#constraint of prototype.constraints" [value]="constraint">
         {{ constraint }}
     </option>
 </select>

我想将每个选择框的开始值设置为静态 string ="Select a option";

I want to set the begin value of every select box to a static string = "Select an option";

推荐答案

ngValue

<select class="form-control" [(ngModel)]="selectedPrototypeSelector" (ngModelChange)="onPrototypeChange()">
   <option *ngFor="let p of prototypes" [ngValue]="p">
         {{ p.id }}
   </option>
</select>    

只需将 selectedPrototypeSelector 设置为您要选择的值即可.

Just set selectedPrototypeSelector to the value you want to have selected.

柱塞示例

这篇关于Angular 2设置选择的开始值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆