Angular2:如何将选定的项目从HTML datalist元素传递回组件?

前端之家收集整理的这篇文章主要介绍了Angular2:如何将选定的项目从HTML datalist元素传递回组件?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个简单的组件,其中包括一个dreate下拉框.这个列表中填充了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));
    }

解决方法

调用方法的(click)事件应该在选中的选项上.您声明的“项目”仅在* ngFor的范围内,因此在其子级别上已知.你声明它太高了.
@H_403_30@

猜你在找的Angularjs相关文章