我创建了一个简单的过滤应用程序,当我在同一个组件(app.ts)中有过滤和列表时:http: //plnkr.co/0eEIJg5uzL6KsI70wWsC
@Component({
selector: 'my-app',
template: `
<select name="selectmake" [(ngModel)]="makeListFilter" (ngModelChange)="selectMake()">
<option *ngFor="let muscleCar of muscleCars" [ngValue]="muscleCar">{{muscleCar.name}}</option>
</select>
<select name="selectmodel" [(ngModel)]="modelListFilter">
<option *ngFor="let m of makeListFilter?.models" [ngValue]="m.model">{{m['model']}}</option>
</select>
<button class="btn btn-default" type="submit" (click)="searchCars()">FILTER</button>
<ul>
<li *ngFor="let _car of _cars | makeFilter:makeSearch | modelFilter:modelSearch">
<h2>{{_car.id}} {{_car.carMake}} {{_car.carModel}}</h2>
</li>
</ul>`,
providers: [ AppService ]
})
export class App implements OnInit {
makeListFilter: Object[];
modelListFilter: string = '';
_cars: ICar[];
constructor(private _appService: AppService) { }
ngOnInit(): void {
this._appService.getCars()
.subscribe(_cars => this._cars = _cars);
}
selectMake() {
if(this.modelListFilter) {
this.modelListFilter = '';
}
}
searchCars() {
this.makeSearch = this.makeListFilter['name'];
this.modelSearch = this.modelListFilter;
}
我想要实现的是将过滤移动到它自己的组件(filtering.component.ts)中,并适当地使用@Input 和@Output 函数将选择框的值传递回列表组件(app.ts)。
我已经在这里分离了组件,但是我无法将数据传递给列表组件。(app.ts): http://plnkr.co/1ZEf1efXOBysGndOnKM5
提前致谢。