Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

128
Visualizações
angular2 two-way-binding of array-type to single value

I'm using a child component to select values which should be updated in my model. The child component accepts an Array<string> via @Input() if I pass in property from my model which is an Array<string> itself everything works as expected an when a value is selected in the child, the model gets updated.

However as an additional requirement I also need to pass in a single-valued (i.e. a regular string, so non-array) property of my model and whenever the user selects a value in the child I'd like to update my single-value model property immediately.

I tried creating the array from the template using [(selectedValues)]=[parentSingleValue] but this fails with

Parser Error: Unexpected token '=' at column 20 in [[parentSingleValue]=$event]

I created a plunkr to illustrate the issue. Click the items to test it: https://plnkr.co/edit/bHCAaycwZLOddry0gOU7

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

There are multiple things wrong here

  1. You don't need the [(selectedValues)]="parentValues" syntax, because there is nothing going from the child to the parent - [selectedValues]="parentValues" will work the same.
  2. It only works because you are passing an array and modifying the array inside the child, so - by side effect. Pass it a copy of the array and it will cease to work.
  3. Your @Output() eventEmitter is unused. It would be used if you did something like [selectedValues]="parentValues" (eventEmitter)="someEventHandlingMethod()"

A solution to what you are trying to achieve would be to simply remove the @Input() parameter, and change the @Output() one to emit a simple string, then in the parent handle it by pushing it into the array and setting the single value at the same time.

Child:

@Output()
onItemSelected = new EventEmitter<string>();

...

public addValue(value: string) {
  this.onItemSelected.emit(value);
}

Parent:

export class App {
  parentValues: Array<string> = [];
  parentSingleValue = '';

  itemSelected(item: string) {
    this.parentValues.push(item);
    this.parentSingleValue = item;
  }
}

Parent template:

<h1>Simply:</h1>
<app-child (onItemSelected)="itemSelected($event)"></app-child>

Single value: {{parentSingleValue}}<br>

<div *ngFor="let value of parentValues">
  <p>{{value}}</p>
</div>
over 4 years ago · Santiago Trujillo Relatório

0

Your code works perfectly fine, the reason is you assigned an empty string.

See modified one

Alternatively,

<app-child [(selectedValues)]="parentValues"></app-child>
<div *ngFor="let value of parentValues">
  <p>{{value}}</p>
</div>

<h1>With singleValue:</h1>
<app-child [selectedValues]="converToArray(parentSingleValue)"></app-child>
<div>
  <p>{{parentSingleValue}}</p>
</div>


export class App {
  title = 'app works!';
  parentValues: Array<string> = [];
  parentSingleValue = '212121';
  converToArray(s:string){
    let temp= new Array();
    temp.push(s);
    return temp

  }
}

LIVE DEMO

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda