我有以下select组件,它来自rest api的数据.如何在md-select上设置默认选择值?
{{warehouse.description}}
Madhu Ranjan.. 20
你可以尝试下面,
组件HTML
{{food.viewValue}} Selected value: {{selectedValue}}
组件脚本
@Component({ selector: 'select-form-example', templateUrl: './select-form-example.html', }) export class SelectFormExample { foods = [ {value: 'steak-0', viewValue: 'Steak'}, {value: 'pizza-1', viewValue: 'Pizza'}, {value: 'tacos-2', viewValue: 'Tacos'} ]; // setting this is the key to initial select. selectedValue: string = this.foods[0].value; }
这里的关键是设置selectedValue
初始值.
检查这个StackBlitz.
希望这可以帮助!!
你可以尝试下面,
组件HTML
{{food.viewValue}} Selected value: {{selectedValue}}
组件脚本
@Component({ selector: 'select-form-example', templateUrl: './select-form-example.html', }) export class SelectFormExample { foods = [ {value: 'steak-0', viewValue: 'Steak'}, {value: 'pizza-1', viewValue: 'Pizza'}, {value: 'tacos-2', viewValue: 'Tacos'} ]; // setting this is the key to initial select. selectedValue: string = this.foods[0].value; }
这里的关键是设置selectedValue
初始值.
检查这个StackBlitz.
希望这可以帮助!!