Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Knockout.js与映射对象绑定并订阅属性更改_Javascript_Knockout.js - Fatal编程技术网

Javascript Knockout.js与映射对象绑定并订阅属性更改

Javascript Knockout.js与映射对象绑定并订阅属性更改,javascript,knockout.js,Javascript,Knockout.js,我有一个JS对象: var bookmark = { id: 'id', description: 'description', notes: 'notes' } 我想绑定到整个对象,在文本区域中显示注释,并订阅对注释的更改 以下是我目前掌握的情况: this.bookmark = ko.observable(); this.bookmark.subscribe = function(bookmarkWithNewNotes) { //use the bookmark

我有一个JS对象:

var bookmark = {
   id: 'id',
   description: 'description',
   notes: 'notes'
}
我想绑定到整个对象,在文本区域中显示注释,并订阅对注释的更改

以下是我目前掌握的情况:

this.bookmark = ko.observable();

this.bookmark.subscribe = function(bookmarkWithNewNotes) {
   //use the bookmarkWithNewNotes.id to update the bookmark in the db
}
我将书签设置为:

this.bookmark(ko.mapping.fromJS(existingBookmark));
视图如下所示:

  <div databind="with: $root.bookmark" >
    Notes
    <textarea class="userNotes" rows="10" data-bind="value: notes" ></textarea>
  </div>
<div>
    Notes
    <div data-bind="foreach: bookmarks">
        <textarea rows="10" data-bind="value: note"></textarea>
    </div>
</div>

笔记
这不管用。我需要做什么才能使它按我希望的方式工作

谢谢

以下是中的一个示例。

你可以这样做:

  <div databind="with: $root.bookmark" >
    Notes
    <textarea class="userNotes" rows="10" data-bind="value: notes" ></textarea>
  </div>
<div>
    Notes
    <div data-bind="foreach: bookmarks">
        <textarea rows="10" data-bind="value: note"></textarea>
    </div>
</div>
获取数据后,为每个项目创建VM,如下所示:

function AppViewModel(data) {
    var self = this;

    self.bookmarks = ko.observableArray();

    for (var i = 0; i < data.length; i++) {
        self.bookmarks().push(new BookmarkViewModel(data[i].id, data[i].description, data[i].note));
    };

    return self;
}

请使用可运行的堆栈代码段(工具栏按钮)更新您的问题,以便我们可以在上下文中轻松查看问题并帮助您解决问题。我只是尝试了,但无法使外部映射文件正常工作。太累了。我会在早上再试一次。顺便说一句,这是数据绑定=“不是数据绑定=”“谢谢。这让我走上了正确的道路,我能够实施一个有效的解决方案。