Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

331
Views
angular2 cambia el valor @input en el componente secundario y luego cambia este valor en el componente principal no funciona

clase de componente principal

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

plantilla de componente principal

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

clase de componente hijo

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

después de activar onClick() en el componente secundario, showChild() no funcionaría para mostrar el componente secundario.

¿por qué?

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

El valor se pasa exclusivamente de padre a hijo porque está usando corchetes.

Para que el valor vaya en ambos sentidos, debe utilizar un enlace de datos bidireccional.

Eso significa que su atributo isShow debería ser como:

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

Y la plantilla debe ser

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

o

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

Eche un vistazo a la página del tutorial de enlace de datos bidireccional:

https://angular.io/guide/template-syntax#enlace-bidireccional---

about 4 years ago · Santiago Trujillo Report

0

Está creando valores no sincronizados entre el elemento secundario y el principal. Dado que el padre está pasando el valor al hijo, debe cambiar el valor solo en el padre. Para enviar un valor del elemento secundario al elemento principal, debe usar un parámetro de Output como EventEmitter . Se verá así:

 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); } }

Esto emite el valor false cuando se ejecuta el método onClick en el elemento secundario. El padre recibe ese nuevo valor y lo asigna a su variable show , que se envía al componente hijo.

about 4 years ago · Santiago Trujillo Report

0

Debe usar un getter y un setter para el valor para que pueda usar la sintaxis de enlace de datos bidireccional. Esto se puede lograr usando lo siguiente:

 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; } }
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!