<tfoot id='z9VKp'></tfoot>
      <bdo id='z9VKp'></bdo><ul id='z9VKp'></ul>

    1. <legend id='z9VKp'><style id='z9VKp'><dir id='z9VKp'><q id='z9VKp'></q></dir></style></legend>

      <small id='z9VKp'></small><noframes id='z9VKp'>

      <i id='z9VKp'><tr id='z9VKp'><dt id='z9VKp'><q id='z9VKp'><span id='z9VKp'><b id='z9VKp'><form id='z9VKp'><ins id='z9VKp'></ins><ul id='z9VKp'></ul><sub id='z9VKp'></sub></form><legend id='z9VKp'></legend><bdo id='z9VKp'><pre id='z9VKp'><center id='z9VKp'></center></pre></bdo></b><th id='z9VKp'></th></span></q></dt></tr></i><div id='z9VKp'><tfoot id='z9VKp'></tfoot><dl id='z9VKp'><fieldset id='z9VKp'></fieldset></dl></div>

    2. 以角度将数据从一个组件传递到另一个组件

      Pass data from one component to another in angular(以角度将数据从一个组件传递到另一个组件)

          <tfoot id='cVC6e'></tfoot>
        • <i id='cVC6e'><tr id='cVC6e'><dt id='cVC6e'><q id='cVC6e'><span id='cVC6e'><b id='cVC6e'><form id='cVC6e'><ins id='cVC6e'></ins><ul id='cVC6e'></ul><sub id='cVC6e'></sub></form><legend id='cVC6e'></legend><bdo id='cVC6e'><pre id='cVC6e'><center id='cVC6e'></center></pre></bdo></b><th id='cVC6e'></th></span></q></dt></tr></i><div id='cVC6e'><tfoot id='cVC6e'></tfoot><dl id='cVC6e'><fieldset id='cVC6e'></fieldset></dl></div>

          <small id='cVC6e'></small><noframes id='cVC6e'>

          • <legend id='cVC6e'><style id='cVC6e'><dir id='cVC6e'><q id='cVC6e'></q></dir></style></legend>
              <tbody id='cVC6e'></tbody>

                <bdo id='cVC6e'></bdo><ul id='cVC6e'></ul>
                本文介绍了以角度将数据从一个组件传递到另一个组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                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="../../../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() {
                  }
                
                }
                

                解决方案

                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
                }
                

                这篇关于以角度将数据从一个组件传递到另一个组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Fetch multiple links inside foreach loop(在 foreach 循环中获取多个链接)
                Backbone Fetch Request is OPTIONS method(Backbone Fetch Request 是 OPTIONS 方法)
                Fetch API leaks memory in Chrome(Fetch API 在 Chrome 中泄漏内存)
                How can I download and save a file using the Fetch API? (Node.js)(如何使用 Fetch API 下载和保存文件?(Node.js))
                Send blob data to node using fetch, multer, express(使用 fetch、multer、express 将 blob 数据发送到节点)
                Sending a custom User-Agent string along with my headers (fetch)(发送自定义用户代理字符串以及我的标头(获取))

              • <i id='lqeVI'><tr id='lqeVI'><dt id='lqeVI'><q id='lqeVI'><span id='lqeVI'><b id='lqeVI'><form id='lqeVI'><ins id='lqeVI'></ins><ul id='lqeVI'></ul><sub id='lqeVI'></sub></form><legend id='lqeVI'></legend><bdo id='lqeVI'><pre id='lqeVI'><center id='lqeVI'></center></pre></bdo></b><th id='lqeVI'></th></span></q></dt></tr></i><div id='lqeVI'><tfoot id='lqeVI'></tfoot><dl id='lqeVI'><fieldset id='lqeVI'></fieldset></dl></div>

                        <legend id='lqeVI'><style id='lqeVI'><dir id='lqeVI'><q id='lqeVI'></q></dir></style></legend>
                      • <small id='lqeVI'></small><noframes id='lqeVI'>

                          <tbody id='lqeVI'></tbody>
                        • <bdo id='lqeVI'></bdo><ul id='lqeVI'></ul>
                          <tfoot id='lqeVI'></tfoot>