简体   繁体   English

Angular父子组件通信

[英]Angular communication between parent and child components

I have a case where I have a parent component and 2 child component (selection components based on ng-select) The idea is that when I select and item in the first ng-select, the same should be disabled in the 2nd.我有一个案例,我有一个父组件和 2 个子组件(基于 ng-select 的选择组件)这个想法是,当我 select 和第一个 ng-select 中的项目时,同样应该在第二个中禁用。 And vice versa.反之亦然。

In the parent I am using @ViewChildren to query the component and the set disabled input property在父我使用@ViewChildren 来查询组件和设置禁用输入属性

In the children I am emitting the information I need in the parent when I choose and item in the ng-select在孩子们中,当我在 ng-select 中选择和项目时,我会在父母中发出我需要的信息

In this method I am then finding the correct child component and setting the disabled input property在这种方法中,我然后找到正确的子组件并设置禁用的输入属性

const connectionSelector = this.connectionSelectors.find(connectionSelector => connectionSelector.source == false);
connectionSelector.disabledConnection = this.selectedSourceConnection;

In the child component I have ngOnChanges method to check for the changes but the disabledConnection property does not get updated在子组件中,我有 ngOnChanges 方法来检查更改,但 disabledConnection 属性没有得到更新

It only works when I make a page load I am both emitting the changes to the parent on OnInit and when I change the item in the ng-select so the changes are emitted to the parent它仅在我进行页面加载时有效

Parent view:父视图:

<sc-connection-selector [default]="false" [source]="true" [quick]="false"
      [selectedConnection]="selectedSourceConnection"
      [disabledConnectionId]="disabledTargetConnectionId"
      (dataToEmit)="onValueChanged($event)">
</sc-connection-selector>

Child:孩子:

<ng-select [items]="connections" bindLabel="userName"
    [placeholder]="'general.select-placeholder' | translate " 
    (change)="onConnectionChange()" 
    groupBy="type"
    dropdownPosition="bottom" 
    [(ngModel)]="selectedConnection">
    <ng-template ng-optgroup-tmp let-item="item">
      <b>{{item.type || 'Unnamed group'}}</b>
    </ng-template>
  </ng-select>

What am I missing here?我在这里想念什么?

-Jani -贾尼

Edit:编辑:

You are updating the value on the child component, no on the parent, OnChanges will trigger if您正在更新子组件上的值,而不是父组件上的值,如果发生 OnChanges 将触发

data-bound property of a directive changes指令的数据绑定属性更改

this means, that when the value on the parent component changes, it will trigger OnChanges on the child component.这意味着,当父组件上的值发生变化时,它将触发子组件上的 OnChanges。

For you to get the desired behavior, you should move the relevant section (where you update the disabled property) from the OnChanges function to the setter of disabledConnectionId, so every time that (disabledConnectionId) changes, you update the dropdown, here is the relevant code:为了获得所需的行为,您应该将相关部分(在其中更新 disabled 属性)从 OnChanges function 移动到 disabledConnectionId 的设置器,因此每次 (disabledConnectionId) 更改时,您都会更新下拉列表,这里是相关的代码:

private currentDisabledConnectionId: Number;
@Input()
set disabledConnectionId(val: number) {
  if (this.currentDisabledConnectionId !== val) {
    this.currentDisabledConnectionId = val;
    this.setDisabledFields();
  }
}

private setDisabledFields() {
  this.connections.find(
    connection => connection.id == this.currentDisabledConnectionId
  ).disabled = true;
  console.log("connections: ", this.connections);
}

Notice I've changed the input to be a setter, and I saved the property as a private field.请注意,我已将输入更改为 setter,并将属性保存为私有字段。

Also note, what you posted on stackblitz doesn't remove previous disabled fields, only adds new ones (I suspect this is because you only pasted a reproduceable example).另请注意,您在 stackblitz 上发布的内容不会删除以前禁用的字段,只会添加新字段(我怀疑这是因为您只粘贴了一个可复制的示例)。

Here is a working forked examlpe on stackBlitz这是stackBlitz上的一个工作分叉示例

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

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