加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 18:05:17 所属栏目:安全 来源:网络整理
导读:我有一个简单的组件,其中包括一个dreate下拉框.这个列表中填充了Web API调用的结果.出于显示目的,我只使用该项目的两个字段.但是,一旦选择了一个元素,我需要对所有其他字段进行处理.如何将整个元素传递回组件? 真的很感激任何帮助. h1Get Locations/h1div d
我有一个简单的组件,其中包括一个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的范围内,因此在其子级别上已知.你声明它太高了.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读