TypescriptのソースとしてRESTサービスを使用して先行入力を行うにはどうすればよいですか? promise を使用する必要がありますか、それともオブザーバブルを使用できますか? 具体的には、ng2-bootstrap ホームページの例から getAsyncData() 関数で必要なものを探しています。
REST 応答:
[{id: 1, name: 'Alabama'}, {id: 2, name: 'Alaska'}, {id: 3, name: 'Arizona'},{id: 4, name: 'Arkansas'}, {id: 5, name: 'California'}]
私が持っているもの(動作しません):
home.ts
import {Component} from 'angular2/core';
import {CORE_DIRECTIVES, FORM_DIRECTIVES} from 'angular2/common';
import {TYPEAHEAD_DIRECTIVES} from '../../../ng2-bootstrap';
import {Http,URLSearchParams,Headers} from 'angular2/http';
// webpack html imports
let template = require('./typeahead-demo.html');
@Component({
selector: 'typeahead-demo',
directives: [TYPEAHEAD_DIRECTIVES, CORE_DIRECTIVES, FORM_DIRECTIVES],
template: template
})
export class TypeaheadDemo {
public selected:string = '';
public asyncSelected:string = '';
public typeaheadLoading:boolean = false;
public typeaheadNoResults:boolean = false;
public states:Array<string> = [];
public statesComplex:Array<any> = [];
public getContext() {
return this;
}
public _cache:any;
public _prevContext:any;
public constructor(public http:Http) {}
public getAsyncData(context:any):Function {
if (this._prevContext === context) {
return this._cache;
}
this._prevContext = context;
let f:Function = function (http:Http) {
let searchParams = new URLSearchParams();
searchParams.set('search', context.asyncSelected);
http.get('http://example.com/search', {search: searchParams})
.subscribe(res => {
context.states = res.json();
});
};
this._cache = f(this.http);
return this._cache;
}
public changeTypeaheadLoading(e:boolean) {
this.typeaheadLoading = e;
}
public changeTypeaheadNoResults(e:boolean) {
this.typeaheadNoResults = e;
}
public typeaheadOnSelect(e:any) {
console.log(`Selected value: ${e.item}`);
}
}
home.html
<div class='container-fluid'>
<pre>Model: {{asyncSelected | json}}</pre>
<input [(ngModel)]="asyncSelected"
[typeahead]="getAsyncData(getContext())"
(typeaheadLoading)="changeTypeaheadLoading($event)"
(typeaheadNoResults)="changeTypeaheadNoResults($event)"
(typeaheadOnSelect)="typeaheadOnSelect($event)"
[typeaheadOptionsLimit]="7"
placeholder="Locations loaded with timeout"
class="form-control">
<div [hidden]="typeaheadLoading!==true">
<i class="glyphicon glyphicon-refresh ng-hide" style=""></i>
</div>
<div [hidden]="typeaheadNoResults!==true" class="" style="">
<i class="glyphicon glyphicon-remove"></i> No Results Found
</div>