如果我有一個看起來像這樣angular2承諾觀測和DI
import {Injectable} from 'angular2/core';
@Injectable()
export class MyService {
search(oSrchParams){
let promise =() =>
new Promise((resolve, reject) =>
Meteor.call('mockSearchWS', (error:any, result:any) => error ? reject(error) : resolve(result))
);
promise().then(
(result:Array<Object>) => {
return result
},
(error) => {
console.log('failed', error);
}
)
}
}
我怎樣才能把它,並得到一個值,當它被注射服務?像這樣:
import {Component} from 'angular2/core';
import {MyService} from './my-service';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/toPromise';
@Component ({
selector: 'search',
providers:[MyService]
})
export class App {
items: Array<string>;
constructor(private myService: MyService) {
}
search(term) {
// this.myService.search(term).then(items => this.items = items);
// this.myService.search(term).subscribe()
}
}
我不確定服務是否是可觀察的。我見過的大多數例子都使用http。
如果你想讓它像'return promise()那樣返回承諾,那麼(' –
是否使用Mongo? –