我想在我的Angular应用启动时填充一个数组,并将其用于Material Autocomplete。我可以从PHP后端接收JSON。在ngOnInit中,我什至可以将其提供给数组并注销。但是,以后我的数组仍未定义。我应该如何正确处理,以便最终将内容显示在我的选项列表中?
app.component.html:
<form class="example-form">
<mat-form-field class="example-full-width">
<input type="text" placeholder="Pick one" aria-label="Number" matInput [formControl]="myControl" [matAutocomplete]="auto">
<mat-autocomplete #auto="matAutocomplete">
<mat-option *ngFor="let option of filteredOptions | async" [value]="option">
{{option}}
</mat-option>
</mat-autocomplete>
</mat-form-field>
</form>
app.component.ts:
import {Component, OnInit, AfterViewInit} from '@angular/core';
import {FormControl} from '@angular/forms';
import {Observable} from 'rxjs';
import {map, startWith, takeUntil, switchMap} from 'rxjs/operators';
import { ServerService } from './server.service';
/**
* @title Filter autocomplete
*/
@Component({
selector: 'app-root',
templateUrl: 'app.component.html',
styleUrls: ['app.component.css'],
})
export class AppComponent implements OnInit {
myControl = new FormControl();
megyek: Observable<string[]>;
filteredOptions: Observable<string[]>;
constructor(private serverService: ServerService) { }
ngOnInit() {
// don't manually subscribe!
this.megyek = this.serverService.getMegyek();
// use switchmap, if user types fast
this.filteredOptions = this.myControl.valueChanges.pipe(
startWith(''),
switchMap(value => this._filter(value))
);
}
private _filter(value: string): string[] {
const filterValue = value.toLowerCase();
return this.megyek
.filter(option => option.toLowerCase().includes(filterValue));
}
}
app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { ReactiveFormsModule, FormsModule } from '@angular/forms';
import { ServerService } from './server.service';
import { HttpModule } from '@angular/http';
import { AppComponent } from './app.component';
import {
MatButtonModule,
MatFormFieldModule,
MatInputModule,
MatRippleModule,
MatAutocompleteModule,
} from '@angular/material';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
@NgModule({
exports: [
MatButtonModule,
MatFormFieldModule,
MatInputModule,
MatRippleModule,
MatAutocompleteModule,
ReactiveFormsModule,
BrowserAnimationsModule,
FormsModule,
HttpModule
],
declarations: [],
imports: []
})
export class MaterialModule {}
@NgModule({
declarations: [
AppComponent
],
imports: [
MaterialModule,
BrowserModule,
],
providers: [ServerService],
bootstrap: [
AppComponent,
],
schemas: [],
})
export class AppModule { }
server.service.ts
import {throwError as observableThrowError, Observable } from 'rxjs';
import {catchError, map} from 'rxjs/operators';
import { Injectable } from '@angular/core';
import { Headers, Http, Response } from '@angular/http';
@Injectable()
export class ServerService {
constructor(private http: Http) {}
storeServers(servers: any[]) {
const headers = new Headers({'Content-Type': 'application/json'});
// return this.http.post('https://udemy-ng-http.firebaseio.com/data.json',
// servers,
// {headers: headers});
return this.http.put('https://udemy-ng-http.firebaseio.com/data.json',
servers,
{headers: headers});
}
getMegyek() {
return this.http.get('http://localhost/Varosok/Controller/ControllerCity.php?content=megyek').pipe(
map(
(response: Response) => {
console.log(response);
const data = response.json();
/*for (const megye of data) {
megye.trim();
}*/
return data;
}
),
catchError(
(error: Response) => {
console.log(error);
return observableThrowError('Something went wrong');
}
), );
}
getAppName() {
return this.http.get('https://udemy-ng-http.firebaseio.com/appName.json').pipe(
map(
(response: Response) => {
return response.json();
}
));
}
}
答案 0 :(得分:3)
请记住,您填充9.1 (mean$24) (mean$25) ..... (mean$32) (mean$35)
的请求是异步的。因此,在执行9.1 (mean$24) (mean$25) ..... (mean$32) (mean$35)
11.9 (mean$24) (mean$25) ..... (mean$32) (mean$35)
13.9 (mean$24) (mean$25) ..... (mean$32) (mean$35)
时,megyek
尚无值,但为AfterViewInit
,因此会引发错误。保持megyek
为可观察状态,不要使用undefined
。因此,尝试:
megyek
同样在过滤器中,您需要使用map来访问数组中的每个字段,而且我似乎还使用AfterViewInit
将其更改为rxjs 6,就像您正在使用它一样。
myControl = new FormControl();
megyek: Observable<string[]>;
filteredOptions: Observable<string[]>;
constructor(private serverService: ServerService) { }
ngOnInit() {
// don't manually subscribe!
this.megyek = this.serverService.getMegyek();
// use switchmap, if user types fast
this.filteredOptions = this.myControl.valueChanges.pipe(
startWith(''),
switchMap(value => this._filter(value))
);
}
演示:StackBlitz
答案 1 :(得分:1)
Your return value of array is any[] and you are now passing to an observable.
Change this (filteredOptions: Observable<string[]>) to (filteredOptions:any[])
change the ngOnit() code to this
(
this.serverService.getMegyek().pipe(**inside here you can use takeUntil to unsubscribe**)
.subscribe(
(megyek: any[]) => {
this.megyek = megyek;
console.log(this.megyek);
},
(error) => console.log(error)
);
)
if you html view use it like this: (
<mat-autocomplete #auto="matAutocomplete">
<mat-option *ngFor="let option of filteredOptions" [value]="option">
{{option}}
</mat-option>
</mat-autocomplete>
*** Don't your array don't come out in a form of key:value? like opion.name and co..
if so, change this {{option}} to something like {{option.name}}
)