gpt4 book ai didi

angular - 我如何从选择的组件中获取选定的值

转载 作者:太空狗 更新时间:2023-10-29 18:12:46 28 4
gpt4 key购买 nike

如何从选择的组件中获取选定的值?

选择.component.ts:

export class PfSelectComponent implements OnInit {

constructor() { }

ngOnInit() {
}

@Input() options : Array<Object>;

}

选择.component.html

<select [(ngModel)]="selectedValue" id="exampleFormControlSelect1" class="form-control">
<option *ngFor="let option of options" [ngValue]="option.value">{{option.name}}</option>
</select>

select value: {{selectedValue}}

{{selectValue}} 不显示在组件中选择的值。

最佳答案

选择.component.html

你应该使用 [value] 而不是 [ngValue] :[ngValue] => [值]

<select [(ngModel)]="selectedValue" id="exampleFormControlSelect1" class="form-control" >
<option *ngFor="let option of options" [value]="option.value">{{option.name}}</option>
</select>

select value: {{selectedValue}}

选择.component.ts

并添加 public selectedValue;

export class PfSelectComponent implements OnInit {

public selectedValue;
constructor() { }

ngOnInit() {
}

@Input() options : Array<Object>;
}

我用

测试了这个
options = [
{
value: 1,
name : "1"
},
{
value: 2,
name : "2"
},
{
value: 3,
name : "3"
}
]

效果不错:)

关于angular - 我如何从选择的组件中获取选定的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49227197/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com