Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 Component @Input two way binding

I have a data driven Angular application. I have a toggle component which I pass in a toggled state. My issue is that the two way data binding does not seem to work unless i pass in the toggle boolean as an object. Is there a way to get this to work without using an EventEmitter or passing the variable in as an object. This is to be a reusable component and the application is heavily data driven so passing the value in as an object in not an option. My code is....

toggle.html

<input type="checkbox" [(ngModel)]="toggled" [id]="toggleId" name="check"/> 

toggle.component.ts

import { Component, Input, EventEmitter, Output } from '@angular/core';  @Component({   moduleId: module.id,   selector: 'toggle-switch',   templateUrl: 'toggle-switch.component.html',   styleUrls: ['toggle-switch.component.css'] })  export class ToggleSwitchComponent {    @Input() toggleId: string;   @Input() toggled: boolean;  } 

parent.component.html

<toggle-switch toggleId="toggle-1" [(toggled)]="nongenericObject.toggled"></toggle-switch> 
like image 303
Steve Fitzsimons Avatar asked Feb 02 '17 15:02

Steve Fitzsimons


People also ask

Is @input two-way data binding?

The answer is "no". In your example, the value that you pass to the @Input property is a reference to an object. If you had two-way binding, you could assign a new object to that property in the child component: this.

What is a two-way data binding?

Two-way data binding refers to sharing data between a component class and its template. If you change data in one place, it will automatically reflate at the other end. For example, if you change the value of the input box, then it will also update the value of the attached property in a component class.

What is the correct way to do two-way data binding in Angular 2?

Angular's two-way binding syntax is a combination of square brackets and parentheses, [()] . The [()] syntax combines the brackets of property binding, [] , with the parentheses of event binding, () , as follows.

How do you do a two-way binding in react?

Let's use LinkedStateMixin to save us some typing: var createReactClass = require('create-react-class'); var WithLink = createReactClass({ mixins: [LinkedStateMixin], getInitialState: function() { return {message: 'Hello!' }; }, render: function() { return <input type="text" valueLink={this.


1 Answers

For [(toggled)]="..." to work you need

  @Input() toggled: boolean;   @Output() toggledChange: EventEmitter<boolean> = new EventEmitter<boolean>();    changeValue() {     this.toggled = !(this.toggled);      this.toggledChange.emit(this.toggled);   } 

See also Two-way binding

[UPDATE] - 25 June 2019
From @Mitch's comment below:
It's worth noting that the @Output name must be the same as the @Input name, but with Change on the end. You can't call it onToggle or something. It's a syntax convention.

like image 166
Günter Zöchbauer Avatar answered Oct 17 '22 08:10

Günter Zöchbauer