Angular Material md-select默认选定值 [英] Angular Material md-select default selected value

查看:86
本文介绍了Angular Material md-select默认选定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Angular 2与 Angular Material 一起使用.查看文档,我试图使select具有与空占位符相对的默认值.

I am using Angular 2 with Angular Material. Looking at the documentation, I am trying to get the select to have a default value as oppose to an empty place holder.

我尝试了以下两个选项,但两个都未设置默认值

I have tried the following two options and both of them does not set a default value

<md-select selected="1">
  <md-option value="1" >One</md-option>
  <md-option value="2">Two</md-option>
</md-select>

<md-select>
  <md-option value="1" selected="true">One</md-option>
  <md-option value="2">Two</md-option>
</md-select>

我查看了所有文档和示例,但没有一个提供帮助

I looked at all the documentations and the examples, non of them helped

推荐答案

使用[(ngModel)]:

<mat-select [(ngModel)]="selectedOption">
  <mat-option value="1">One</mat-option>
  <mat-option value="2">Two</mat-option>
</mat-select>

组件:

selectedOption ='1';

selectedOption = '1';

演示

编辑#1:

因为 此PR ),您可以使用MatSelectvalue 属性 选择一个值:

Since Material2.0.0#beta10 (specifically this PR) you can select a value using the value property of MatSelect:

<mat-select [value]="selectedOption">
  <mat-option value="1">One</mat-option>
  <mat-option value="2">Two</mat-option>
</mat-select>

组件:

selectedOption ='1';

selectedOption = '1';

请注意,您也可以将其与双向数据绑定->一起使用. [(值)] .

Note that you can also use it with two-way data binding -> [(value)].

演示

这篇关于Angular Material md-select默认选定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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