简体   繁体   English

如何在Angular 5中设置child(或nth child)组件的child的属性

[英]How to set property of child of child(or nth child) component in Angular 5

How can I set property of child of child(or nth level child) component from parent in Angular 5 without creating intermediate variables. 如何在Angular 5中从父级设置子级(或n级子级)组件的子级属性,而无需创建中间变量。 For example lets say I've child component as shown below. 例如,假设我有子组件,如下所示。

@Component({
  selector: 'app-child11',
  template: '<p><br/> child11 works!<br/> Name is {{name}}</p>',
  styleUrls: ['./child11.component.css']
})
export class Child11Component implements OnInit {

  @Input() name: string;
  constructor() {}
  ngOnInit() {}
}

Let us say I've parent of child11 is child1 as shown below 假设我的child11的父母是child1,如下所示

@Component({
  selector: 'app-child1',
  template: '<p>child1 works!<app-child11 name="Karthik"></app-child11></p>',
  styleUrls: ['./child1.component.css']
})
export class Child1Component implements OnInit {
  constructor() {}

  ngOnInit() {}
}

And the parent component as shown belown 父组件如下所示

@Component({
  selector: 'app-root',
  template: '<div><app-child1></app-child1></div>',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app';
}

Now how can I modify template of app-root such that I can directly set the name value of child11 in app-root, without introducing variable in Child1 that propagates to AppComponent? 现在,如何修改app-root的模板,以便可以直接在app-root中设置child11的名称值,而无需在Child1中引入传播到AppComponent的变量?

If I wanted to do this , I would prefer to use binded component input instead of simple input. 如果我想这样做,我宁愿使用绑定的组件输入,而不是简单的输入。

The code that I prepared is. 我准备的代码是。

app.component.ts app.component.ts

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

@Component({
  selector: 'app-child11',
  template: '<p><br/> child11 works!<br/> Name is {{name}}</p>',
  styleUrls: []
})
export class Child11Component implements OnInit {

  @Input() name: string;
  constructor() { }
  ngOnInit() { }
}

@Component({
  selector: 'app-child1',
  template: '<p>child1 works!<app-child11 [name]="name"></app-child11></p>',
  styleUrls: []
})
export class Child1Component implements OnInit {
  constructor() { }
  @Input() name: string;
  ngOnInit() { }
}

@Component({
  selector: 'app-root',
  templateUrl: 'app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app';
  name: string;
}

app.component.html app.component.html

<p>child1 works!
    <app-child11 [name]="name"></app-child11>
</p>
<br>
<input type="text" [(ngModel)]="name"/>

Now whatever you write inside input tag in app-root component will be reach to app-child11 and finally it will print there. 现在,无论您在app-root组件的input标签中编写的内容如何,​​都将到达app-child11,最后将在此打印。

I hope it can help you. 希望对您有所帮助。

if you dont want creating intermediat variables you can create Service for example ChildService . 如果您不想创建中间变量,则可以创建Service ,例如ChildService Service Guide . 服务指南 Create child$: Observable<any[]> or child$$: Subject<any[]> and subscribe on Child11Component : 创建child$: Observable<any[]>child$$: Subject<any[]>并订阅Child11Component

this.child$
    .filter(child => child.name === this.name)
    .subscribe(child => this.child = child);

Your this.child variable has necessary child. 您的this.child变量有必要的孩子。

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

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