简体   繁体   English

Angular:如何使用 select 默认情况下 select 中的第一个元素,当使用复杂 ZA8CFDE633149EB2ACZ66F9 时

[英]Angular: how to select by default the first element in a select when a complex object is used

I'm developing a web application using Angular 11. A component receives an array with object via input:我正在使用 Angular 11 开发 web 应用程序。组件通过输入接收带有 object 的数组:

[
   {propery1: "a", property2:"b"},
   {propery1: "c", property2:"d"},
   {propery1: "e", property2:"f"},
]

So In my_component.ts file I have:所以在 my_component.ts 文件中我有:

@Input() myArray: any[];
private selectedObject: any; // The current object selected with all its properties!

I would like selectedObject to be the entire object (of the array) selected via a dropdown menu.我希望selectedObject是通过下拉菜单选择的整个 object (数组的)。 I did it like this:我是这样做的:

<select [(ngModel)]="selectedObject">
    <option *ngFor="let object of myArray;" [ngValue]="object">{{object.property1}}</option>
</select>

The selectedObject is linked with the current value object . selectedObject与当前值object相关联。 I got what I wanted, but I can't understand why the dropdown appears on the screen with no options selected :我得到了我想要的,但我不明白为什么下拉菜单出现在屏幕上而没有选择任何选项

在此处输入图像描述

If I open the menu, the results appear, as expected:如果我打开菜单,结果会按预期出现:

在此处输入图像描述

And if I select a property, the "empty property" disappears:如果我 select 一个属性,“空属性”消失:

在此处输入图像描述

I wish this didn't happen.我希望这没有发生。 I would like the 'a' value to be visible right from the start.我希望从一开始就可以看到'a'值。 This can be easily achieved by changing the value of ngValue with object.property1 .这可以通过使用object.property1更改ngValue的值来轻松实现。 But this way the binding wouldn't work as I want it to ( selectedObject must be the full object and no one property).但是这种方式绑定不会像我想要的那样工作( selectedObject必须是完整的 object并且没有一个属性)。 How do I make the first property appear without this problem occurring?如何在不出现此问题的情况下使第一个属性出现?

In your.ts file:在你的 .ts 文件中:

  myArray = [
    {property1: "a", property2:"b"},
    {property1: "c", property2:"d"},
    {property1: "e", property2:"f"},
 ]
 public selectedValue: string 
 public selectedObject: any

 ngOnInit() {
   this.selectedValue = this.myArray[0].property1
   this.onValueChange();
 }

 onValueChange() {
   const found = this.myArray.filter( obj => obj.property1 === this.selectedValue)
   this.selectedObject = found? found[0] : {}
 }

In your.html file在你的.html 文件中

<select [(ngModel)]="selectedValue" (ngModelChange)="onValueChange()">
<option *ngFor="let item of myArray"
        [value]="item.property1">{{item.property1}}</option>
</select>

<p>selectedvalue : {{selectedValue}}</p>

<p *ngIf="selectedObject">selected object : {{selectedObject.property1}} ; {{selectedObject.property2}}</p>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Angular 选择元素中设置默认选项 - How to set default option in Angular select element 在 Angular 7 的下拉列表中选择第一个元素 - Select first element in dropdown in Angular 7 Angular 2选择具有初始值的复杂对象 - Angular 2 select complex object with initial value 怎么做 <select>元素不选择默认选择的第一个选项? - How to make <select> element not to choose first option as selected by default? 默认情况下如何选择第一个值? - How to select the first value by default? 如何在Angular中选择动态填充/未填充选择框的第一个元素? - How to select the first element of dynamically filled/unfilled Selectbox in Angular? 如何在不使用[]的情况下在IE8的javascript中选择对象的第一个元素? - How to select the first element of an object in javascript on IE8 without using []? Angular 8: Select ng-content 的第一个元素 - Angular 8: Select first element of ng-content Angular总是在选择元素中选择第一个选项 - Angular always selects the first option in select element 获取返回值[object Object]以获取第一个结果的下拉列表。 当我选择一个选项并且有效时它会改变。 如何修复默认结果 - Getting return Value [object Object] for dropdown at first result. It changes when i select an option and works. How to fix the default result
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM