項目要求從給定的URL取得數(shù)據(jù),然后列到Dropdown組件的select list中。我嘗試著從service中用HttpClient得到數(shù)據(jù),并傳給Component,結(jié)果怎么也不成功,請問到底哪里出問題了?各位給指點一下迷津吧。
主要代碼如下:
//data.service.ts
......
@Injectable()
export class DataService {
private dataUrl = 'https://library.com/data.....';
private list: Library[];
......
constructor(private http: HttpClient) {}
public listData(pattern: string): Observable<Library[]> {
this.getList(this.dataUrl);
return of(this.list
.filter((library) => library.name.toUpperCase().indexOf(pattern.toUpperCase()) !== -1)
.sort(this.sortFunction));
}
getList(urlStr: string) {
this.http
.get(urlStr)
.subscribe((data: Library[]) => {
if (data) {
this.list = data;
} else {
this.list = null;
}
});
}
......
}
//dropdown.component.ts
......
import {DataService} from '../../service/data.service';
@Component({
selector: 'app-dropdown',
templateUrl: './dropdown.component.html'
})
export class DropdownComponent implements OnInit {
public listItems: (item: string) => Observable<Library[]>;
ngOnInit() {
this.listItems = (term: string) => this.DataService.listData(term);
}
......
}
在Html中就是列出一個dropdown組件,下拉列表中就應(yīng)該是取得的listItems。但問題是沒有取得任何數(shù)據(jù),下拉列表中是空的,點擊任何一個都顯示undefined。
謝謝啦先!
因為listItems是異步拿到的,所以你在綁定dropdown數(shù)據(jù)源的時候,可以有兩種方式:
<dropdown [data]="listItems"></dropdown>
listItems: Library[];
getList(urlStr: string) : void {
this.http
.get(urlStr)
.filter((data: Library[]) => data !== null)
.subscribe((data: Library[]) => {
this.listItems = data.filter((library) => library.name.toUpperCase().indexOf(pattern.toUpperCase()) !== -1)
.sort(this.sortFunction))
});
}
<dropdown [data]="listItems | async"></dropdown>
listItems: Observable<Library[]>;
getList(urlStr: string) : Observable<Library[]> {
this.http
.get(urlStr)
.filter((data: Library[]) => data !== null)
.map((data: Library[]) => {
return data.filter((library) => library.name.toUpperCase().indexOf(pattern.toUpperCase()) !== -1)
.sort(this.sortFunction))
});
}
this.listItems = getList("url");謝謝@腎導的回答!出差剛回來,抱歉答謝遲了。
兩種方法都試了一下,結(jié)果都不行(主要是本人技術(shù)實在太淺)。在filter處報錯提示:Property 'filter' does not exist on type Observable<Object>. import {filter} from 'rxjs/operator/filter'; 也不行,把filter刪除后雖未報錯,但提取不到數(shù)據(jù)。
用下面兩種方法試了下,顯示listDropdownItems可以得到數(shù)據(jù),但是卻不能放到Dropdown的下拉列表里。不知道問題出在哪里?
//dropdown.component.ts
...
public getItems: (ids: string[]) => Observable<Library[]>;
public listItems: (item: string) => Observable<Library[]>;
public listItemsMax: (item: string, ids: string[]) => Observable<Library[]>;
public entityToSelectItem: (entity: Library) => SelectItem;
public count: number;
urlStr = 'https://.../api/dict/Library/...';
public listDropdownItems: Library[];
......
ngOnInit() {
this.dropdownService.getMembers(this.urlStr).subscribe((data: Library[]) => {
if (data !== null) {
this.listDropdownItems = data;
} else {
this.listDropdownItems = null;
}
});
this.initDropdownItem();
}
private initDropdownItem() {
this.listItems = (term: string) => this.listData(term);
this.listItemsMax = (term: string, ids: string[]) => {
const selectedCount = ids ? ids.length : 0;
return this
.listDataMax(term, 3 + selectedCount)
.pipe(
tap(response => this.count = response.count),
map((response) => response.results)
);
};
this.getItems = (ids: string[]) => this.getItemData(ids);
this.entityToSelectItem = (entity: any) => {
return {
id: entity.id,
text: entity.libraryDisplay,
entity: entity
};
};
}
listData(pattern: string, maxResults?: number): Observable<Library[]> {
return of(this.listDropdownItems
.filter((library) => library.libraryDisplay.toUpperCase().indexOf(pattern.toUpperCase()) !== -1)
.sort(this.sortFunction));
}
sortFunction(library1: Library, library2: Library) {
if (library1.libraryDisplay < library2.libraryDisplay) {
return -1;
}
if (library1.libraryDisplay > library2.libraryDisplay) {
return 1;
}
return 0;
}
listDataMax(pattern: string, maxResults: number): Observable<{ count: number, results: Library[] }> {
const filteredList = this.listDropdownItems
.filter((library) => library.libraryDisplay.toUpperCase().indexOf(pattern.toUpperCase()) !== -1)
.sort(this.sortFunction);
return timer(200)
.pipe(map((t) => {
return {
count: filteredList.length,
results: maxResults && maxResults < filteredList.length ? filteredList.splice(0, maxResults) : filteredList
};
}));
}
getItemData(ids: string[]): Observable<Library[]> {
const selectedItems: Library[] = [];
ids.forEach((id) => {
this.listDropdownItems
.filter((item) => item.id === id)
.map((item) => selectedItems.push(item));
});
return of(selectedItems);
}
.......
//dropdown.service.ts
import {HttpClient, HttpHeaders} from '@angular/common/http';
@Injectable()
export class DropdownService {
private headers: HttpHeaders = new HttpHeaders().set('Content-Type', 'application/json');
private options = {headers: this.headers};
constructor(private http: HttpClient) { }
getMembers(urlStr: string) {
return this.http
.get(urlStr);
}
}
//dropdown.component.html
//下面數(shù)據(jù)都顯示出來了。
<div>
<ul *ngIf="listDropdownItems">
<li *ngFor="let listDropdownItem of listDropdownItems;">
<p>
ID: <span>{{listDropdownItem.id}}</span>
ModuleName: <span>{{listDropdownItem.modulename}}</span>
LibraryDisplay: <span>{{listDropdownItem.libraryDisplay}}</span>
</p>
</li>
</ul>
</div>
//但是下面的Dropdown下拉列表里顯示不出來,是空的,點擊下拉框沒有任何反應(yīng)。
<form [formGroup]="form">
<div class="form-group col-md-6">
<h2>dropdown-select</h2>
<app-dropdown cssStyle="form-control input-sm"
placeholder="Type to search..."
[dataProvider]="listItems"
[selectItemAdapter]="entityToSelectItem"
[selectedProvider]="getItems"></app-dropdown>
</div>
</div>
</form>
但如果把listDropdownItems定義成固定的Library[]數(shù)組,而不是從Url取到的,如下,則上面的Dropdown下拉列表是沒有問題的。
listDropdownItems: Library[] = [
{
id: '3',
modulename: 'libraryApp',
libraryDisplay: 'libraryApp'
}, {id: '4',
modulename: 'Test',
libraryDisplay: 'Test'}
];
請各位老師們再指教一下吧!
北大青鳥APTECH成立于1999年。依托北京大學優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
達內(nèi)教育集團成立于2002年,是一家由留學海歸創(chuàng)辦的高端職業(yè)教育培訓機構(gòu),是中國一站式人才培養(yǎng)平臺、一站式人才輸送平臺。2014年4月3日在美國成功上市,融資1
北大課工場是北京大學校辦產(chǎn)業(yè)為響應(yīng)國家深化產(chǎn)教融合/校企合作的政策,積極推進“中國制造2025”,實現(xiàn)中華民族偉大復興的升級產(chǎn)業(yè)鏈。利用北京大學優(yōu)質(zhì)教育資源及背
博為峰,中國職業(yè)人才培訓領(lǐng)域的先行者
曾工作于聯(lián)想擔任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔任項目經(jīng)理從事移動互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍懿科技有限責任公司從事總經(jīng)理職務(wù)負責iOS教學及管理工作。
浪潮集團項目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺面向?qū)ο箝_發(fā)經(jīng)驗,技術(shù)功底深厚。 授課風格 授課風格清新自然、條理清晰、主次分明、重點難點突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗。曾經(jīng)歷任德國Software AG 技術(shù)顧問,美國Dachieve 系統(tǒng)架構(gòu)師,美國AngelEngineers Inc. 系統(tǒng)架構(gòu)師。