0
イムにアクセスするとき:のcontentEditableスパンでの変更を保存するには、奇妙な方法を試したが、私は少し問題を抱えているのViewModelオブジェクトに
これは私のスパンである:
<span data-bind="text: Content, event: { blur: viewModel.contentEdited}" contenteditable></span>
とViewModelには、以下のような:
function note(id, date, content, category, color, background) {
this.ID = id;
this.Date = date;
this.Content = content;
this.Category = category;
this.Color = color;
this.Background = background;
}
:テンプレートがあるノートに結合され
var viewModel = {
notes: ko.observableArray([]),
newNoteContent: ko.observable(),
actualId: 0
};
viewModel.contentEdited = function() {
alert(this.notes);
}
アラートに「未定義」と表示されているのは、それが、viewModelの別のインスタンスを作成し、そこにメモが空であるためですか?テンプレートの文脈はメモであり、それはviewModelの外にあります。
ありがとうございました!
ありがとうございました!だから私はviewModelにバインドするときに "this"の "context"を変更します。しかし、依然としてノート自体にアクセスしたい場合は、パラメータなどで送ることができると思います。 –
はい、あなたは通常、 'this'をあなたのビューモデルにしたいと思っています。 –