角度服务不起作用

时间:2018-01-29 04:03:36

标签: angular typeahead

我正在两个组件之间建立服务,但它没有按预期工作。你能告诉我什么是遗失的吗?当用户从typeahead中选择值时,该值将传递给搜索组件以查找该选定值的详细信息。 typeahead和search是两个不同的组件。

 /* html */
 <input id="typeahead-http" type="text" class="form-control" [class.is-invalid]="searchFailed" (ngModelChange)="onCapture()" [(ngModel)]="model" [ngbTypeahead]="search" placeholder="control" />


 /* service */
 import { Injectable } from '@angular/core';
 import { BehaviorSubject } from 'rxjs/BehaviorSubject';
 @Injectable()
 export class DataService {
   private keyword = new BehaviorSubject<string>("abc");
   currentKeyword = this.keyword.asObservable();

   constructor() { }
   changeKeyword(message: string) {
      this.keyword.next(message)
    }   
  }


 /* component that sends data */
 export class MedtypeaheadComponent {
 constructor(private _service: WikipediaService, private data: 
  DataService) {}

 onCapture() {
     this.data.changeKeyword("happy");
 }

}

/* component receives data */
export class SearchComponent {

rows = [];
keyword = null;
constructor(private http: Http, private data: DataService) {}
onSearch(): void {
    this.data.currentKeyword.subscribe(keyword => this.keyword = keyword );
    var data = { 'search' : this.keyword };
    console.log(data);
}
  }

0 个答案:

没有答案