简体   繁体   English

angular2更改子组件中的@input值然后更改父组件中的此值不起作用

[英]angular2 change @input value in child component then change this value in parent component not work

parent component class 父组件类

export class Parent {
   show: boolean = false;
   constructor() { }
   showChild() {
      this.show = true;
   }
}

parent component template 父组件模板

<child [isShow]="show"></child>

child component class 子组件类

export class Child {
   @Input isShow: boolean = false;
   constructor() { }
   onClick() {
      this.isShow = false;
    }
}

after I triggered onClick() in child component, the showChild() would not work to show the child component. 在我触发子组件中的onClick()后,showChild()无法显示子组件。

why? 为什么?

The value is being passed exclusively from parent to child because you're using square brackets. 由于您使用的是方括号,因此该值仅从父级传递给子级。

In order for the value to go both ways, you need to use a two-way data binding. 为了使值双向,您需要使用双向数据绑定。

That means your isShow attribute should be like: 这意味着你的isShow属性应该是这样的:

@Input()  isShow: boolean;
@Output() isShowChange = new EventEmitter<boolean>();

And the template should be 模板应该是

<child [(isShow)]="show"></child>

or 要么

<child [isShow]="show" (isShowChange)="show = $event"></child>

Take a look at the two-way data binding tutorial page: 看一下双向数据绑定教程页面:

https://angular.io/guide/template-syntax#two-way-binding--- https://angular.io/guide/template-syntax#two-way-binding---

You are creating values out of sync between the child and parent. 您正在创建子级和父级之间不同步的值。 Since the parent is passing the value down into the child, you need to change the value in the parent only. 由于父级将值传递给子级,因此您只需更改父级中的值。 To send a value from the child to the parent you need to use an Output parameter as an EventEmitter . 要将值从子节点发送到父节点,您需要使用Output参数作为EventEmitter It will look like this: 它看起来像这样:

export class Parent {
   show: boolean = false;
   constructor() { }
   showChild() {
      this.show = true;
   }
}

<child [isShow]="show" (updateValue)="show = $event"></child>



export class Child {
   @Input isShow: boolean = false;
   @Output() updateValue = new EventEmitter();

   constructor() { }
   onClick() {
      this.updateValue.emit(false);
    }
}

This emits the value false when the onClick method in the child runs. 当子onClick中的onClick方法运行时,这会发出false值。 The parent receives that new value and assigns it to it's show variable, which gets sent down to the child component. 父级接收该新值并将其分配给它的show变量,该变量将被发送到子组件。

You need to use a getter and setter for the value so you can use the two-way data binding syntax. 您需要为值使用gettersetter ,以便可以使用双向数据绑定语法。 This can be accomplished using the following: 这可以使用以下方法完成:

export class Child {
    private isShowValue = false;

    @Input()
    public get isShow(){
        return this.isShowValue;
    }

    @Output() isShowChange = new EventEmitter();

    set isShow(val) {
        this.isShowValue = val;
        this.isShowChange.emit(this.isShowValue);
    }

    constructor() { }

    onClick() {
        this.isShow = false;
    }
}

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

相关问题 更改父组件Angular的输入值? - Change input value from parent component Angular? Angular2-在另一个组件中更改组件值 - Angular2 - Change Component value in another component Angular2 组件:测试表单输入值变化 - Angular2 Component: Testing form input value change 在angular2中将父组件的值传递给ngForm的子组件? - Passing value from Parent component to child component for ngForm in angular2? 从父组件到子组件的角度输入值不起作用 - Angular input value from parent to child component doesn't work 在父组件中单击按钮时如何更改子组件的@input值(子组件位于* ngfor循环中) - How to change the @input value of child component when button click in parent component ( child component is in *ngfor loop ) 如何从angular2的子组件更改父变量 - How to change parent variable from child component in angular2 子组件中的Angular @Input也会更新父组件中的值 - Angular @Input in child component also updates value in parent component Angular4 @Input()将值从父组件传递到子组件 - Angular4 @Input() to pass value from parent component to child component 在Angular 4中,当我们处于子组件中时,如何更改父组件的值? - In Angular 4 how we can change the parent component value when we are in child component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM