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>
@Output
decorated EventEmitter has to be named as the corresponding@Input
with a Change suffix at the end as in: @Input() toggled: boolean; @Output() toggledChange: EventEmitter<boolean> = new EventEmitter<boolean>(); – Andreas