我試圖在Angular2中使用ngModel實現內聯編輯。我有一個數組需要使用ngFor迭代,也使用ngModel。當我嘗試對該數組應用內嵌編輯時,我只能編輯每個數組變量的一個字符。Angular2使用ngModel和ngFor的內聯編輯
你可以找到一個工作示例here。
這裏有一個地方我使用ngModel和ngFor合的部件代碼:
import {Component} from '@angular/core'
import {InlineEditComponent} from './inline.component';
@Component({
selector: 'inline-app',
providers: [],
template: `
<div>
<h2>Inline Editing with Angular 2</h2>
<inline-edit [(ngModel)]="editableText" (onSave)="saveEditable($event)"></inline-edit>
</div>
<div>
<ul style="margin:5px;">
<li ngFor #arr [ngForOf]="array" [ngForTrackBy]="customTrackBy">
<inline-edit [(ngModel)]="arr" (onSave)="saveEditable($event)"></inline-edit>
</li>
// <li style="margin:5px;" *ngFor="let arr of array ; let i=index">
// <inline-edit [(ngModel)]="array[i]" (onSave)="saveEditable($event)"></inline-edit>
// </li>
</ul>
</div>
`,
directives: [InlineEditComponent]
})
export class InlineApp {
customTrackBy(index: number, obj: any): any {
return index;
}
editableText = "Click to edit me!";
// Save name to the server here.
saveEditable(value){
console.log(value);
}
array=['bmw','benz','honda'];
}
如果有人可以幫助我,這將是巨大的。
我原以爲是發生了什麼事情與@ Varun的的問題...你的解決方案是相當不錯的,但取消行動不再取消...它仍然保存了所做的任何更改 –
是的,我沒有對所有其他功能進行理智檢查,應該在那裏發出警告。我的改變純粹是爲了解決他所遇到的問題:) –
實質上,他的解決方案涉及製作一個模型 - 「數據」類。這是實施任何可重複數據對象時推薦的方式。 – BrianRT