gpt4 book ai didi

javascript - 选择数组angular 7 safari第一个索引的下拉值默认值

转载 作者:行者123 更新时间:2023-12-04 10:37:09 27 4
gpt4 key购买 nike

我正在使用 Safari 并尝试将数组第一个索引作为选择下拉列表中的默认选项,但不知何故默认情况下它不显示任何选项。我不想在代码方面处理这个问题,因为有些值可能来自服务。

我试过 selected="selected"但没有帮助。

有什么办法可以让我们直接将选项设为默认值?还是手动?

下面是我的代码和 stackblitz。

https://stackblitz.com/edit/angular-7aqzj2

@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
constructor(
public form: FormBuilder
) {}
name = 'Angular 5';
selectedCountry: any;
public dropdown: FormGroup;
countrydrodown = [];
cities = {};
places = {};
texts = {};
countriesValue = [{
'id': 1,
'name': 'France'
}, {
'id': 2,
'name': 'Germany'
}, {
'id': 3,
'name': 'Italy',
}]
countries = [{
'id': 1,
'name': 'France',
'cities': ['Paris'],
'places': [
'effil tower',
'new discover'
],
'texts': ['a']
},
{
'id': 2,
'name': 'Germany',
'cities': ['Hamburg'],
'places': [],
'texts': ['b']
},
{
'id': 3,
'name': 'Italy',
'cities': ['Roma', 'Milan', 'Napoli'],
'places': [
'effil tower',
'new discover'
],
'texts': ['c']
},
];

ngOnInit() {
this.onChange(1);
this.serviceGetCountries();
this.dropdown = new FormGroup({
country: new FormControl(),
city: new FormControl(),
place: new FormControl(),
text: new FormControl()

});
}
getCountries() {
return Observable.of(this.countriesValue);
}

serviceGetCountries() {
this.getCountries().subscribe(res => {
this.countrydrodown = res;
console.log(this.countrydrodown);
});
}

onChange(deviceValue) {
console.log(deviceValue);
this.getValues(deviceValue).subscribe(res => {
console.log(res);
this.cities = res.filter(x => x.id == deviceValue)[0].cities;
this.places = this.countries.filter(x => x.id == deviceValue)[0].places;
this.texts = this.countries.filter(x => x.id == deviceValue)[0].texts;
console.log(Object.keys(this.places));
console.log(this.cities);
});
}

getValues(val) {
return Observable.of(this.countries);


}
}
<div [formGroup]="dropdown">
<div class="col">
<div class="row">
<select formControlName="country" class="rounded-inputs20 select-select col-md-3" (change)="onChange($event.target.value)">
<option selected *ngFor="let country of countrydrodown" [value]="country.id" selected="selected">{{country.name}}</option>
</select>
</div>
<div *ngIf="cities?.length > 0" class="row">
<div style=" padding-top: 0.5em; ">
<select formControlName="city" id="sel1" class="">
<option *ngFor="let city of cities" [ngValue]="city" selected="selected">{{city}}</option>
</select>
</div>
</div>
<div *ngIf="places?.length > 0" class="row">
<div style=" padding-top: 1em; ">
<label *ngFor="let place of places">
<input formControlName="place" type="radio">{{place}}
</label>
</div>
</div>
<div *ngIf="texts?.length > 0" class="row">
<div style=" padding-top: 1em; ">
<label *ngFor="let text of texts">{{text}} :
<input formControlName="text" type="text">
</label>
</div>
</div>
</div>

</div>

最佳答案

这是我的解决方案

On your template


<select formControlName="country" class="rounded-inputs20 select-select col-md-3" (change)="onChange($event.target.value)">
<option value = "default" selected> Select Cities </option>
<option *ngFor="let country of countrydrodown" [value]="country.id">{{country.name}}</option>
</select>

<select formControlName="city" id="sel1" class="">
<option value = "default" selected> Select Cities </option>
<option *ngFor="let city of cities" [ngValue]="city">{{city}}</option>
</select>

On your Typescript, add this:


public defaultSelect = "default";

我希望这有助于

[编辑]

当您申报属性(property)时 defaultSelect = "default"<option>里面的值将设置为 "default"还有那个 first option is selected . Select Cities在下拉列表中将始终首先被选中并显示

关于javascript - 选择数组angular 7 safari第一个索引的下拉值默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60116966/

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