【发布时间】:2017-06-19 19:02:15
【问题描述】:
我有一个数据驱动的 Angular 应用程序。我有一个切换组件,我以切换状态传递。我的问题是,除非我将切换布尔值作为对象传递,否则双向数据绑定似乎不起作用。有没有办法让这个工作不使用 EventEmitter 或将变量作为对象传递。这是一个可重用的组件,并且应用程序是大量数据驱动的,因此将值作为对象而不是选项传递。我的代码是......
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>
【问题讨论】:
-
我想强调米奇对此评论的重要性。要使用父组件的 var 进行双向绑定,必须将
@Output装饰的 EventEmitter 命名为相应的@Input,并在末尾加上 Change 后缀,如:@Input( ) 切换:布尔值; @Output() toggledChange: EventEmitter= new EventEmitter ();
标签: angular typescript data-binding components decorator