简体   繁体   English

将数据从子组件传递到父组件Angular4

[英]pass data from child component to parent component Angular4

I have lists of apis which are stored in a server: ServerGetData, like this: 我有存储在服务器ServerGetData中的api列表,如下所示:

apis = {
 "Repo Assignment": "https://apis.repoAssignment",
 "Status Summary": "https://apis.statsSummary",
 ...
}

I have created a child component drop down menu: TitleDropdownComponent child component, which display all api titles: "Repo Assignment"... In my parent component, I wanted to render different data tables based on the title which is selected from child component. 我创建了一个子组件下拉菜单:TitleDropdownComponent子组件,其中显示所有api标题:“回购分配” ...在我的父组件中,我想根据从子组件中选择的标题来呈现不同的数据表。

Right now, I can successfully get titles from child component and print them in the console in parent component, However, in my ngOnInit(), the same variable can not be changed accordingly, always get same api data because of title variable does not change. 现在,我可以成功地从子组件中获取标题,并在父组件的控制台中打印它们,但是,在我的ngOnInit()中,相同的变量无法相应地更改,因为标题变量不会更改,所以始终获取相同的api数据。 How can I change the title variable in ngOnInit or using other way like ngOnChanges? 如何更改ngOnInit中的标题变量或使用ngOnChanges等其他方式? Please see my parent app component below. 请在下面查看我的父应用程序组件。 Thanks in advanced! 提前致谢!

import { Component, Input, EventEmitter, Output, ViewChild, OnInit } from '@angular/core';
import { ServerGetData } from './server.getData';
import { TitleDropdownComponent } from './TitleDropdown/titleDropdown.component'


@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent implements OnInit {
  // make ajax call from ServerGetData

  constructor(private dataService: ServerGetData) {}

  data = {};

  // get Child component variables

  @ViewChild(TitleDropdownComponent)
  private titleComponent: TitleDropdownComponent;

  onSelected(selected: string) {
    // default child component title is "Repo Assignment", in here overwrite 
    // titleComponent.title to be selected title
    this.titleComponent.title = selected;
    // each time can the selected title can be printed in console
    console.log(this.titleComponent.title);
    return this.titleComponent.title;
  }

  ngOnInit(): void {

   // *** No idea why in here this.titleComponent.title is not able to change accordingly and always is default title: "Repo Assignment" ***

    this.dataService.getData(this.titleComponent.title).subscribe(
      (data) => {
        this.data = data.items;
      }
    );
  };

}

The ngOnInit is only always executed one time when the component is initialized. 初始化组件时,始终只执行一次ngOnInit It won't later re-execute as other selections are made. 稍后将不会在执行其他选择时重新执行。

If you need to reget the data each time a new item is selected, move the code to within the onSelected method. 如果每次选择新项目时都需要获取数据,则将代码移到onSelected方法内。

  onSelected(selected: string) {
    // default child component title is "Repo Assignment", in here overwrite 
    // titleComponent.title to be selected title
    this.titleComponent.title = selected;
    // each time can the selected title can be printed in console
    console.log(this.titleComponent.title);

    this.dataService.getData(this.titleComponent.title).subscribe(
      (data) => {
        this.data = data.items;
      }
    );

    return this.titleComponent.title;
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM