简体   繁体   English

如何在 angular 中将对象数组从子组件传递到父组件

[英]How to pass Array of Objects from Child Component to Parent Component in angular

I created two components and named it as parent and child.我创建了两个组件并将其命名为父子组件。 And I linked these components at app.component.html.我在 app.component.html 链接了这些组件。 Now I have an array of objects in child component, My goal is to show those array of objects in parent component by using @Output.现在我在子组件中有一个对象数组,我的目标是使用@Output 在父组件中显示这些对象数组。 My Expected output is Each object should be in a separate div.我预期的 output 是每个 object 应该在一个单独的 div 中。

If I am not clear with my doubt please put a comment.如果我不清楚我的疑问,请发表评论。

This is app.component.html这是 app.component.html

<div class="container">
  <div class="row">
    <div class="col-6">
      <app-parent></app-parent>
    </div>
    <div class="col-6">
      <app-child></app-child>
    </div>
  </div>
</div>

This is app.component.css这是 app.component.css

There is no css in this

This is childcomponent.html这是子组件。html

<div class="childclass">
    <h1>This is Child</h1>
</div>

This is childcomponent.css这是子组件。css

.childclass {
    background-color: burlywood;
}

This is childcomponent.ts这是 childcomponent.ts

import { Component, OnInit, EventEmitter, Output } from '@angular/core';

@Component({
  selector: 'app-child',
  templateUrl: './child.component.html',
  styleUrls: ['./child.component.css']
})


export class ChildComponent implements OnInit {
  employs = [
    {
      name: `Mark`,
      age: 27,
      jobProfile: 'React Developer'
    },
    {
      name: 'Williams',
      age: 29,
      jobProfile: 'Angular Developer'
    },
    {
      name: 'Tom',
      age: 32,
      jobProfile: 'Vuejs Developer'
    }
  ]

  @Output()
  outputFromChild: EventEmitter<any> = new EventEmitter<any>()
  constructor() { }

  ngOnInit() {
  }

}

This is parentcomponent.html这是父组件。html

<div class='parentclass'>
    <h1>This is Parent</h1>
</div>

This is parentcomponent.css这是父组件。css

.parentclass {
    background-color: aqua;
}

This parentcomponent.ts这个父组件.ts

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-parent',
  templateUrl: './parent.component.html',
  styleUrls: ['./parent.component.css']
})
export class ParentComponent implements OnInit {

  constructor() { }

  ngOnInit() {
  }

}

Make use of EventEmitter such that you should be able to communicate from child to parent.使用EventEmitter以便您应该能够从孩子到父母进行交流。

app.component.html app.component.html

<div class="container">
  <div class="row">
    <div class="col-6">
      <app-parent [employees]="employeesFromChild"></app-parent>
    </div>
    <div class="col-6">
      <app-child (ouputFromChild)="getEmployees($event)"></app-child>
    </div>
  </div>
</div>

app.component.ts app.component.ts

employeesFromChild: any;
getEmployees(event){
  this.employeesFromChild = event; //here you will get the employees from child
}

parent.component.ts父组件.ts

@Input() employees: any;

ngOnInit(){
  console.log(this.employees);
}

child.component.ts child.component.ts

 employs = [
    {
      name: `Mark`,
      age: 27,
      jobProfile: 'React Developer'
    },
    {
      name: 'Williams',
      age: 29,
      jobProfile: 'Angular Developer'
    },
    {
      name: 'Tom',
      age: 32,
      jobProfile: 'Vuejs Developer'
    }
  ]

 @Output()
  outputFromChild: EventEmitter<any> = new EventEmitter<any>()

 ngOnInit() {
     this.ouputFromChild.emit(employs);
  }

My goal is to show those array of objects in parent component by using @Output我的目标是使用 @Output 在父组件中显示这些对象数组

Its' better to use a service along with @Output because parent component and child component are siblings to each other.最好将服务与@Output一起使用,因为parent componentchild component是彼此的兄弟。

Service.ts服务.ts

export class ServiceName {
    private employeeData= new BehaviorSubject<any>();

    getEvent(): Observable<any> {
        return this.employeeData.asObservable();
    }

    setEvent(param: any): void {
        this.employeeData.next(param);
    }
  }

There are few steps you need to follow to complete your requirement,您需要遵循几个步骤来完成您的要求,

  1. Store the data in Service employeeData object, once this done call the Parent class (here it is AppComponent) with help of EventEmitter.将数据存储在 Service employeeData object 中,一旦完成,在 EventEmitter 的帮助下调用 Parent class(这里是 AppComponent)。
  2. Set up an input field from App Component to Parent Component(which are parent and child in your case).设置从应用程序组件到父组件的输入字段(在您的情况下是父组件和子组件)。
  3. Once the Input field gets updated then call Service employeeData object to retrive data in Parent Component.输入字段更新后,调用 Service employeeData object 以检索父组件中的数据。

Have a look here to know more about data sharing between sibling components in Angular在这里查看以了解有关data sharing between sibling components in Angular更多信息

Hope this helps you.. :)希望这对你有帮助.. :)

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

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