fstanisの回答は技術的に正しいです。しかし、私は彼が逃したものに少し追加しました。 OPはコンポーネントレベルのプロパティで結果を保持していましたが、返された結果を直接使用するのではなく、後で使用することができます。私はこれを達成するもう一つの可能な方法をこの答えに加えています。
1)の方法のOPはそれをやろうとしました。これは、あなたのサービスが
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import {Observable} from 'rxjs/Rx';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
@Injectable()
export class MyDataService {
constructor(private http:Http) { }
getData(){
return this.http.get('some/api/url').map(
res => res.json()
);
}
}
希望のようになります方法です
export class FooComponent implement OnInit {
myData:any = [];
...
ngOnInit() {
this.myDataService.getData().subscribe(
data => {
this.myData = data;
$('.ui.search').search({ source: this.myData });
},
error => {
// Write your logic here if an error was occurred. Use the variable "error" which has info about what happened.
},
() => {
// This fires when the call has been finished. Just like .complete in jQuery.get or jQuery.post. So that you can do necessary operations after everything is completed.
}
);
}
}
購読2角度を使用して
export class FooComponent implement OnInit {
myData:any = [];
...
ngOnInit() {
this.myDataService.getData().then(myData => {
this.myData = myData;
$('.ui.search').search({ source: this.myData });
});
}
}
2)これは役に立ちます。
私はドキュメントでそのリンクを読んだことがありますが、検索コンポーネントに "Angular2 way"(約束)を適用する方法がわかりません。手伝って頂けますか? – smartmouse