简体   繁体   English

淘汰赛js不从视图模型更新

[英]knockout js not updating from view model

So I have a the following textarea in my view that I am attempting to update via a knockout binding. 因此,在我看来,我尝试通过淘汰赛绑定来更新以下文本区域。

Here is the code from the View: 这是视图中的代码:

<textarea disabled id="CCHPISentence" style="width:99.7%; height:75px; resize: none; font-family:Verdana; overflow: auto;" data-bind="text: fullSymptomTextObservable"> </textarea>

Here is the Jquery function that applies the bindings, I am wondering if my issue is here: 这是应用绑定的Jquery函数,我想知道我的问题是否在这里:

   $(function () {
    ko.applyBindings(symptomTextViewModel, document.getElementById("CCHPISentence"))
})

Here is my ViewModel: 这是我的ViewModel:

function symptomTextViewModel(fullText) {
if (fullText === undefined)
{ fullText = ""}
this.fullSymptomTextObservable = ko.observable(fullText.toString())

} }

Here is a snip from the js function that calls my ViewModel. 这是调用我的ViewModel的js函数的一个片段。 I am building the fullText variable in this 2nd js function: 我在第二个js函数中构建fullText变量:

    //FINAL PARAGRAPH KNOCKOUT VM MAPPING
fullText = sentence1 + sentence2 + sentence3 + sentence4 + sentence5
var symptSentViewModel = new symptomTextViewModel(fullText)
symptomTextViewModel(fullText);

Thanks so much to anybody in advance who can assist me here. 非常感谢任何可以在这里为我提供帮助的人。 I feel like I am missing something stupid, I have tried this every different way that I can think of with no luck. 我觉得自己缺少一些愚蠢的东西,我尝试了所有我想不到的运气。

it would be easier to make fullSymptomTextObservable a pureComputed observable. fullSymptomTextObservable pureComputed可观察值会更容易。 That way as the various sentences change so will the full sentence. 这样,随着各种句子的变化,整个句子也会发生变化。 This way you are taking advantage of knockoutjs. 这样,您就可以利用敲除js的优势。

 function SymptomTextViewModel(fullText) { var self = this; if (fullText === undefined) { fullText = "" } self.fullSymptomTextObservable = ko.observable(fullText.toString()) } var vm = new SymptomTextViewModel('This is a test paragraph. Watch for the alert();'); ko.applyBindings(vm,document.getElementById("CCHPISentence")); alert("about to use the vm variable to access the text."); vm.fullSymptomTextObservable('Changing it to something else'); alert("about to use the ko.dataFor function"); var testVm = ko.dataFor(document.getElementById("CCHPISentence")); testVm.fullSymptomTextObservable("Maybe this would be better suited to what you need."); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script> <textarea disabled id="CCHPISentence" style="width:99.7%; height:75px; resize: none; font-family:Verdana; overflow: auto;" data-bind="text: fullSymptomTextObservable"></textarea> 

First of all I would fix all missing semicolons in your code. 首先,我将修复代码中所有缺少的分号。 Than check if scope of 'this' in your view models function is correct. 比在您的视图模型函数中检查“ this”的范围是否正确。 Some info from browser console would be also helpful. 来自浏览器控制台的一些信息也将有所帮助。 Check if there isn't any errors which knockout would throw. 检查是否没有任何敲除会引发的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM