如果我有一个看起来像这样的服务
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。