简体   繁体   English

Angular 4 Firebase下拉选择标签选项

[英]Angular 4 firebase dropdown select tag option

I am using Angular 4 and firebase. 我正在使用Angular 4和firebase。 I'm trying to build a dropdown selector for a list of properties. 我正在尝试为属性列表构建一个下拉选择器。

Once a property is selected from the dropdown list, the property location should appear in another input field below. 从下拉列表中选择属性后,属性位置应出现在下面的另一个输入字段中。

properties.component.html properties.component.html

 <select [(ngModel)]="selectedProperty" (change)="onSelect($event, property)"> <option>--select property--</option> <option *ngFor="let property of properties">{{property.propertyName}}</option> </select> <div *ngIf="selectedProperty"> <label >Location </label> <input type="text" value="{{selectedProperty.location}}"> </div> 

properties.component.ts properties.component.ts

 import { Component, OnInit } from '@angular/core'; import { PropertyService } from './../services/property.service'; import { Property } from './../models/property'; import { Observable } from 'rxjs/Observable'; @Component({ selector: 'app-property-list', templateUrl: './property-list.component.html', styleUrls: ['./property-list.component.scss'] }) export class PropertyListComponent implements OnInit { properties: Observable<Property[]>; selectedProperty: Property; constructor(private propertyService: PropertyService) {} ngOnInit() { this.propertyService.getProperties().subscribe(properties => { this.properties = properties; }) } onSelect(event, property: Property){ this.selectedProperty = property; } } 

I am able to select the property from the dropdown list but the property location does not appear on the input field. 我可以从下拉列表中选择属性,但是属性位置不会出现在输入字段中。 I'll appreciate your help. 多谢您的协助。

Instead of value use ngModel 代替值使用ngModel

<div *ngIf="selectedProperty">
    <label >Location </label>
    <input type="text"  [(ngModel)]="selectedProperty.location">
</div>

By default selecting option from dropdown selects whatever value provided in selected option tag. 默认情况下,从下拉菜单中选择option选择所选option标签中提供的任何值。 So when you select any value in dropdown it puts propertyName inside respective ngModel . 因此,当您在下拉列表中选择任何值时,会将propertyName放在各自的ngModel

As you want to select the whole object use ngValue in option, what that will do is, when user selects an option it will take down ngValue object value and assigned it to ngModel of select field. 当您要选择整个对象时,请使用ngValue选项,当用户选择一个选项时,它将ngValue对象值并将其分配给select字段的ngModel。

<option [ngValue]="property" *ngFor="let property of properties">
  {{property.propertyName}}
</option>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM