【发布时间】:2019-05-16 21:16:54
【问题描述】:
我有以下 HTML,并希望在 input 或 my-component 更改时同步 myModel。
<input type="text" [(ngModel)]="myModel" />
<my-component [(ngModel)]="myMdoel></my-component>
有什么想法吗?
编辑:
<div *ngFor="let item of items">
<input type="text" name="item{{item.name}}" [(ngModel)]="item.name" />
<my-component [(model)]="item.name" [datasource]="source"></my-component>
</div>
我的组件
<button type="button" *ngFor="let s of datasource" (click)="selectItem(s)">{{s}}</button>
export class MyComponent ... {
_model: any;
@Input()
get model(): any {
return this._model;
}
@Input()
set model(value: any) {
this._model = vaue;
}
@Output() modelChange: EventEmitter<any> = new EventEmitter<any>();
@Input() datasource: any[];
...
selectItem(item: any): void {
this._model = item;
this.modelChange.emit(this._model);
}
}
【问题讨论】:
-
你能分享我的组件代码
标签: angular angular-ngmodel ngmodel