2つの入力フィールドがあり、ユーザーはPickupおよびDeliveryエリア名を入力します。 (配信またはピックアップ - シーケンスなし)私は両方の入力値がJSONファイルから検索され、両方の入力の一致が見つかった場合、関連する価格を表示するプロセスを開始したいと思います。私は両方の入力で値を取得することができますが、私はJSONファイルと成功するショー価格と比較する方法が必要です。コンストラクタで定義されたメソッドで、Jsonファイル内のすべてのオブジェクトを取得します。Jsonファイル内の2つの入力フィールド値を検索して一致する場合、同じjsonオブジェクトから3番目のKey値を返します
注。 MATCH PROCESSがCLICKで始まらず、むしろ値の(変更)から始まります。あなたはONCを使用することができます
テンプレート
<div class="form-group">
<input class="form-control" id="pickupSuburb" (change)="onSelectedSuburb($event)" placeholder=" Pickup Suburb Here" [(ngModel)]="pickupSuburb" >
</div>
<div class="form-group">
<input list="suburb" class="form-control" id="deliverySuburb" (change)="onSelectedSuburb($event)" placeholder=" Delivery Suburb Here" [(ngModel)]="deliverySuburb" >
</div>
コンポーネント
import {Component, OnInit, ElementRef } from '@angular/core';
import 'rxjs/add/operator/map'
import { FormControl } from '@angular/forms';
import {Http} from '@angular/http';
@Component({
selector: 'app-sending',
templateUrl: './sending.component.html',
styleUrls: ['./sending.component.css'],
providers: [SuburbsService, AutoCompleteSuburbs, CapitalizePipe ],
})
export class SendingComponent implements OnInit {
priceList = [];
constructor(private elm: ElementRef, private http: Http) {
http.get('./assets/zoneRates.json').subscribe(response => {
this.priceList = response.json();
})
}
ngOnInit() {}
public onSelectedSuburb(event) {
const pickupArray = this.elm.nativeElement.querySelector('#pickupSuburb').value.slice(1).slice(-3);
const deliveryArray = this.elm.nativeElement.querySelector('#deliverySuburb').value.slice(1).slice(-3);}
JSONサンプル
[{
"pickup": "SYD",
"delivery": "QC4",
"weight": "25",
"price": "6.25"
}, {
"pickup": "SYD",
"delivery": "QC6",
"weight": "25",
"price": "6.25"
}, {
"pickup": "SYD",
"delivery": "QC7",
"weight": "25",
"price": "6.25"
}, {
"pickup": "SYD",
"delivery": "ADL",
"weight": "25",
"price": "6.25"
}]
パーフェクト。ありがとう –