public function getCityIdAttribute($value)
{
return is_null($value) ? " " : $value;
}
我希望在export class RulesComponent {
items: Observable<Array<string>>;
term = new FormControl();
constructor(private searchService: SearchService) {}
ngOnInit() {
this.items = this.term.valueChanges
.debounceTime(400)
.distinctUntilChanged()
.switchMap(term => this.searchService.search('rules', term));
}
}
@Injectable()
export class SearchService {
constructor(private jsonp: Jsonp) {}
search(name: string, term: string) {
var search = new URLSearchParams();
search.set('search', term);
return this.jsonp
.get('http://127.0.0.1:8000' , { search })
.map(this.extractData)
.json();
}
}
值更改之前调用search(),并在页面加载时将term
值设置为搜索结果,但我无法弄清楚如何。
答案 0 :(得分:3)
老问题,但今天我解决了同样的问题。正确的解决方案是:
ngOnInit() {
this.items = this.term.valueChanges
.startWith('')
.debounceTime(400)
.distinctUntilChanged()
.switchMap(term => this.searchService.search('rules', term));
this.term.setValue('search text here', {emitEvent:true});
}
重要的是.startWith(initialValue)
部分将为observable发出初始值。更多描述
in ReactiveX documentation
答案 1 :(得分:0)
你应该能够在这样的组件的ngOnInit()函数中触发搜索。
width
链接到A2 FormControl文档以供参考: https://angular.io/docs/ts/latest/api/forms/index/FormControl-class.html
答案 2 :(得分:0)
试试这个:
this.term.setValue("New York");