Knockout.js, set observable value manually from de

2019-07-29 06:08发布

问题:

Giving the following setup:

<textarea id="textarea-chat" type="text" data-bind="value: chatMessage" /> 

I'd like to open the developer console and manually set the value of chatMessage

document.getElementById('textarea-chat').value = 'somevalue';

Will change the content of the of the DOM element, but not the observer value. Accessing chatMessage() results in undefined

Suggestions are welcome

回答1:

Taking the ideas from here and here, you need to access the value of chatMessage through the ViewModel, which is available in the console when you use the ko.dataFor() method. So, the steps would be:

  1. var vm = ko.dataFor(document.body)
  2. vm.chatMessage('somevalue')


标签: knockout.js