Knockout.js,从开发者控制台手动设置可观察值 [英] Knockout.js, set observable value manually from developer console

查看:50
本文介绍了Knockout.js,从开发者控制台手动设置可观察值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

提供以下设置:

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

我想打开开发者控制台并手动设置chatMessage的值

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

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

将更改DOM元素的内容,但不会更改观察者值. 访问chatMessage()会导致undefined

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

欢迎提出建议

推荐答案

此处,您需要通过ViewModel访问chatMessage的值,该视图在使用ko.dataFor()方法时在控制台中可用.因此,步骤将是:

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')
  1. var vm = ko.dataFor(document.body)
  2. vm.chatMessage('somevalue')

这篇关于Knockout.js,从开发者控制台手动设置可观察值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆