我正在开发一个包含搜索功能的应用程序。
目前在应用程序中有两个组件: 1.导航栏 2.搜索网格列表
导航栏组件包含一个文本框,您可以在其中输入搜索查询并按回车键,此组件将进行API调用并获取数据。 当数据返回时,我希望在SearchGridList组件中的数组中填充此数据。
我很难理解在Angular中如何在组件之间传递数据,请有人看看我的代码并指导我。
navbar.component.ts
import { Component, OnInit, Input, Output } from '@angular/core';
import {DataService} from '../../services/data.service';
import {SearchResults} from '../class/search.class';
import {SearchGridListComponent} from '../search-grid-list/search-grid-list.component';
import { EventEmitter } from '@angular/core';
@Component({
selector: 'app-navbar',
templateUrl: './navbar.component.html',
styleUrls: ['./navbar.component.css']
})
export class NavbarComponent implements OnInit {
searchQuery : String;
//searchResultList : Array<any> = [];
constructor(private dataService : DataService) { }
doSearch () : any
{
this.dataService.doSQLSearch(this.searchQuery)
.then ((data:any)=>{
for (var i =0; i<data.Results.length;i++){
let searchObj = new SearchResults(data.Results[i]);
//I want to push data into array from SearchGrid like this
resultGridList.push(searchObj);
}
});
}
ngOnInit() {
}
}
导航栏组件.html
<mat-toolbar class="main-header">
<a href="/">
<img src="../../../assets/vms-header-logo.png" id= "header-logo">
</a>
<form class="search-box">
<mat-form-field class="search-box-full-width">
<input id ="search-textbox" matInput placeholder="Enter a Barcode, DSID or any search term" name="Search" [(ngModel)]="searchQuery" (keyup.enter)="doSearch()" autocomplete="off">
</mat-form-field>
</form>
</mat-toolbar>
搜索网格组件.search-grid.component.ts
import { Component, OnInit, Input } from '@angular/core';
import {NavbarComponent} from '../navbar/navbar.component';
@Component({
selector: 'app-search-grid-list',
templateUrl: './search-grid-list.component.html',
styleUrls: ['./search-grid-list.component.css'],
})
export class SearchGridListComponent implements OnInit {
resultGridList : Array <any> = [];
constructor() { }
ngOnInit() {
}
}