从<select>标记返回数字

时间:2018-06-21 09:09:11

标签: javascript html angular

我试图在选择标记内获取选项,以数字而不是字符串的形式返回。这是我的html:  
                           
我以为将类型设置为数字会成功,但事实并非如此!我正在使用的功能是:   searchPropertiesByBedrooms(浴室:浴室){     console.log('bathrooms:',bathrooms);     this.properties = this._propertyManage.searchPropertiesByBedrooms(bathrooms).valueChanges();   } 但是,当我将其用作输入字段时,它的确会将其作为数字返回,但是由于某些原因,使用select标记时,会将其作为字符串返回。

2 个答案:

答案 0 :(得分:1)

创建一个数字数组,然后使用ngFor对其进行循环。然后使用[ngValue]分配值。

<select type="number" name="bathrooms" [(ngModel)]="bathrooms">
           <option *ngFor="let item of items" [ngValue]="item"  > {{item}}</option> 
 </select>

items: number[] = [1,2,3,4]

答案 1 :(得分:0)

我认为它是value=3,所以没有引号。