简体   繁体   English

父组件的角度5设置子组件属性

[英]angular 5 setting child component property from parent component

I want to set the child component property from parent component 我想从父组件设置子组件属性

parent.component.html parent.component.html

<div>
<child-detail #myCarousel [childList]="detail.List" ></child-detail>
</div>  

parent.component.ts parent.component.ts

@Component({
  selector: "parent",
  templateUrl: "./parent.component.html",
 })  
export class AppComponent { 
  public detail; // it is of JSON type
}

child.component.ts child.component.ts

@component({
 selector:'child-detail',
 templateUrl: './child.component.html'
})
export class ChildDetailComponent{
  @Input() public childList;   // that list of array type
}

child.component.html child.component.html

<div *ngFor = "let list of childList">
 <li>{{list}} </li>
</div>

I know i can set this property by @Input() binding , but this will set only at the time of initializing of the component , I want to set it programmaticaly in intermediate at some point . 我知道我可以通过@Input()绑定来设置此属性,但这只能在组件初始化时设置,我想在某个时候以编程方式在中间设置它。

I know I can access this child property in parent using 我知道我可以使用

@ViewChild(ChildDetailComponent) childDetailComponent: ChildDetailComponent;
console.log(this.childDetailComponent.childList);

But I want to set it programatically at any event of parent. 但是我想以编程方式在任何父事件中设置它。

I'm not sure how you're doing it without using the @Input property. 我不确定在不使用@Input属性的情况下如何做。

Whenever the input you've passed to it changes, it will also reflect in the Child Component. 每当您传递给它的输入发生更改时,它也将反映在子组件中。 You can track it in ngOnChanges in the Child Component. 您可以在子组件的ngOnChanges中对其进行跟踪。 Just try log ging to the console 只需尝试logconsole

Give this a try: 试试看:

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

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  detail: any = {
    childList: []
  };

  setChildList() {
    this.detail.childList = [
      { id: 1, name: 'John' },
      ...
    ];
  }

}

And in the template: 并在模板中:

<button (click)="setChildList()">Set Child List</button>
<hello [childList]="detail.childList"></hello>

Here the @Input property is changing on the Click of the Button. 在这里, @Input属性在“单击按钮”上更改。 But in your case, you can simply call the setChildList method your event. 但就您而言,您可以简单地将setChildList方法调用为事件。


Here's a Working Sample StackBlitz for your ref. 这是供您参考的工作样本StackBlitz

use @input in child component. 在子组件中使用@input can listen to the changes in child component using ngOnChanges . 可以使用ngOnChanges监听子组件中的ngOnChanges

@Input() name: string;

ngOnChanges(changes: SimpleChanges) {
  const name: SimpleChange = changes.name;
  console.log('prev value: ', name.previousValue);
  console.log('got name: ', name.currentValue);
}

working stackblitz 工作堆闪电战

You can use service for the sharing data pragmatically. 您可以通过务实的方式将服务用于共享数据。

Step 1 : Create data share service. 步骤1 :创建数据共享服务。

import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';

@Injectable()
export class DataService {

  private messageSource = new BehaviorSubject('default message');
  currentMessage = this.messageSource.asObservable();

  constructor() { }

  changeMessage(message: string) {
    this.messageSource.next(message)
  }

} 

Step 2: Change message 步骤2:变更讯息

export class ParentComponent implements OnInit {

  message:string;

  constructor(private data: DataService) { }

  ngOnInit() {
   this.data.changeMessage("Hello from Sibling")
  }

Step 3: Get message 步骤3:取得讯息

export class ChildComponent implements OnInit {

  message:string;

  constructor(private data: DataService) { }

  ngOnInit() {
    this.data.currentMessage.subscribe(message => this.message = message)
  }

}

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

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