gpt4 book ai didi

html - Angular 2 : How do you pass a selected item from a HTML datalist element back to the component?

转载 作者:太空狗 更新时间:2023-10-29 16:45:04 27 4
gpt4 key购买 nike

我有一个简单的组件,其中包括一个用于创建下拉框的组件。这是一个列表,其中包含 Web API 调用的结果。出于显示目的,我只使用项目的两个字段。但是,一旦选择了一个元素,我就需要对所有其他字段进行处理。如何将整个元素传递回组件?

非常感谢任何帮助。

<h1>Get Locations</h1>
<div>
<div>
<input list="browsers" name="browser" #term (keyup)="search(term.value)">
<datalist id="browsers">
<option *ngFor="let item of items | async" >
{{item.code + " " + item.description}}
</option>
</datalist>
</div>
<input type="submit" value="Click" (click)="onSelect(item)" />
</div>

组件代码如下:

import { Component, OnInit }        from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { Subject } from 'rxjs/Subject';
import { LocationService } from './location.service';
import {Location} from './location.component';
import './rxjs-operators';

@Component({
selector: 'lesson-08',
templateUrl: './views/lesson08.html',
providers: [LocationService]
})
export class Lesson08 implements OnInit{

constructor(private locationService: LocationService) { }

aLoc: Location;

ngOnInit() {
this.aLoc = new Location();
}

errorMessage: string;
locations: Location[];
mode = 'Observable';
displayValue: string;

private searchTermStream = new Subject<string>();

search(term: string) {
this.searchTermStream.next(term);
}

onSelect(item: Location) {
// do stuff with this location

}

items: Observable<Location[]> = this.searchTermStream
.debounceTime(300)
.distinctUntilChanged()
.switchMap((term: string) => this.locationService.search(term));
}

最佳答案

调用该方法的(点击)事件应该在选中的选项上。您声明的“项目”仅在 *ngFor 的范围内为人所知,因此在其子级别上也是如此。您声明的级别太高了。

关于html - Angular 2 : How do you pass a selected item from a HTML datalist element back to the component?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39247156/

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