简体   繁体   English

如何从 angular 中的另一个组件更新输入装饰器值

[英]How to update input decorator value from another component in angular

I am trying to remove Test2 value from an input decorator value from the change component.我正在尝试从更改组件的输入装饰器值中删除 Test2 值。 But not able to remove it.但无法删除它。 so how to get solution for this.那么如何解决这个问题。

change component:更改组件:

export class ChangeComponent implements OnInit {
@ViewChild('changeComponent') changeComponent: TableComponent;
constructor() {}

ngOnInit() {}

 changeName() {
 this.changeComponent.names.pop('Test2');

 console.log(this.changeComponent.names);
  }
  }

Demo: https://stackblitz.com/edit/angular-pass-table-data-to-input-property-qvwy2q?file=src%2Fapp%2Fapp.component.ts演示: https://stackblitz.com/edit/angular-pass-table-data-to-input-property-qvwy2q?file=src%2Fapp%2Fapp.component.ts

There are a lot of ways for components to communicate with each other.组件相互通信的方式有很多种。 In that example I would create a service that will be used to hold and manipulate the state.在该示例中,我将创建一个服务,用于保存和操作 state。

Here's a link: https://stackblitz.com/edit/angular-ivy-fzd5rx这是一个链接: https://stackblitz.com/edit/angular-ivy-fzd5rx

I suggest to form your question to be: how to update an input value in a component from a sibling component?我建议将您的问题形成为:如何从同级组件更新组件中的输入值?

Answer回答

A component itself can not directly change an @Input value in other siblings components.组件本身不能直接更改其他兄弟组件中的@Input值。

Here are some ways to do this:以下是执行此操作的一些方法:

My suggested way我建议的方式

Implement the ControlValueAccessor on your change component to enable the ngModel two way binding on it.在您的更改组件上实现ControlValueAccessor以在其上启用ngModel双向绑定 And then pass your variable (names) in the two way binding ngModel of your change component, which will take care of changing that value on action and emits the changed value back up to the parent component.然后通过更改组件的两种方式绑定ngModel 传递您的变量(名称),这将负责更改操作中的值并将更改后的值发送回父组件。 That is all you need.这就是你所需要的。 Next time the change detection is triggered, the change will apply and your preview component will get the new value through the @Input.下次触发更改检测时,将应用更改,并且您的预览组件将通过 @Input 获取新值。

Example:例子:

In your change component:在您的更改组件中:

import { Component, forwardRef} from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';

@Component({
  selector: 'app-change',
  templateUrl: './change.component.html',
  styleUrls: ['./change.component.css'],
  providers: [
    {
      provide: NG_VALUE_ACCESSOR,
      useExisting: forwardRef(() => ChangeComponent),
      multi: true,
    },
  ],
})
export class ChangeComponent implements ControlValueAccessor {
  names;
  onChange;
  onTouched;
  
  writeValue(obj: any): void {
    this.names = obj;
  }
  registerOnChange(fn: any): void {
    this.onChange = fn;
  }
  registerOnTouched(fn: any): void {
    this.onTouched = fn;
  }

  changeName() {
    this.names.pop('Test2');
    this.onChange(this.names.slice());
    console.log(this.names);
  }
}

In your parent component template:在您的父组件模板中:

<button (click)="onEnter()">Set Name</button>

<app-table [names]="names"> </app-table>

<app-change [(ngModel)]="names"> </app-change>

This example is very similar to the @Input @Output way to communicate between components, where you change an @Input value between siblings by emitting the new value from the change component up to the parent and the parent will automatically pass the new value the preview component when the angular change detection is triggered.此示例非常类似于组件之间通过 @Input @Output 进行通信的方式,您可以通过将新值从更改组件发射到父级来更改兄弟姐妹之间的 @Input 值,父级将自动将新值传递给预览当触发 angular 变化检测时的组件。

Here is my stackblitz example这是我的stackblitz示例

Another way to do this另一种方法来做到这一点

Provide a service on top of all the siblings and define your value to change in your service and the inject your service in the change sibling and the preview sibling components and change the value directly in the service, this way does not require the the angular @Input decorator.在所有兄弟姐妹之上提供服务并定义您的值以更改您的服务并将您的服务注入更改兄弟姐妹和预览兄弟姐妹组件并直接在服务中更改值,这种方式不需要 angular @输入装饰器。

At the end checkout angular documentation about the components interaction for better understanding.最后检查 angular 有关组件交互的文档,以便更好地理解。

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

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