Pass data from one component to another in angular(以角度将数据从一个组件传递到另一个组件)
问题描述
I am working on a application which contains a search functionality.
Right now I have 2 components in application 1. Navbar 2. SearchGridList
Navbar component contains a text box, in which you type in a search query and hit enter and this component will make a api call and get the data. When the data comes back, I want to populate this data in an array in SearchGridList component.
I am having a tough time understanding passing data within components in Angular, can someone please take a look at my code and guide me.
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() {
}
}
navbar.component.html
<mat-toolbar class="main-header">
<a href="/">
<img src="Li4vLi4vLi4vYXNzZXRzL3Ztcy1oZWFkZXItbG9nby5wbmc=" 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() {
}
}
You need to add to navbar following @Output
event:
export class NavbarComponent implements OnInit {
...
@Output() public found = new EventEmitter<any>();
...
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]);
this.found.emit(searchObj); // !!!! here emit event
// however emitting events in loop looks strange... better is to emit one evet
}
});
}
...
}
Ok in your grid component use @Input
as resultGridList
parameter
export class SearchGridListComponent implements OnInit {
@Input() public resultGridList : Array <any> = [];
...
}
Ok and now in your App component join this two in following way
App template html:
<app-navbar (found)="handleResults($event)"></app-navbar>
<app-search-grid-list [resultGridList]="data"></app-search-grid-list>
And in App ts file:
data = [];
...
handleResults(searchObj) {
this.data = searchObj
}
这篇关于以角度将数据从一个组件传递到另一个组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:以角度将数据从一个组件传递到另一个组件


- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- Fetch API 如何获取响应体? 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- 400或500级别的HTTP响应 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01