在视图模型内单击时更新输入框的文本-剔除 [英] Update an Input Box's text on a click inside the viewmodel - knockout
问题描述
我有一个有关更新当前绑定的嵌套视图模型的问题,这与我的最后一个问题此处有关.
I have a question regarding updating currently bound nested viewmodels and that is related to my last question here.
这是一个测试,我添加了一个简单的输入"框来采样当前问题.
This is for a test where I added a simple Input box to sample my current problem.
问题是:
ViewModel绑定在模板内部(不要认为示例一定如此) 在按钮上单击,我们要更新位于其中的输入框,该输入框将不会更新,我确实通过控制台获取了报告,并且似乎数据已更改.您的想法是什么,小提琴位于这个问题的底部.
ViewModel is bound inside a template(Don't think the sample should necessarily be) On a button click we want to update the Input Box located inside it, The input box won't be updated, I did getting reports by console and seems the data is changed. What is your idea, Fiddle is located at the bottom of this question.
<div data-bind="foreach: Groups">
<input data-bind="value:newText" /> ...
目标在一行中:
self.newText = 'Added'; // were it doesn't take effect
包含手动映射的ViewModel:
function groupViewModel(data) {
var self = this;
self.newText = ko.observable('');
ko.mapping.fromJS(data, {}, self);
self.addPerson = function(personData) {
// here should be your ajax request, I'll use dummy data again
self.People.push({Name: "Michael", LastName: "Bay"});
*self.newText = 'Added';*
//ko.mapping.fromJS(data, {}, self);
};
}
var mapping = {
"Groups" : {
'create': function(options) {
return new groupViewModel(options.data);
}
}
};
整个代码位于小提琴上:
The whole code is located on fiddle:
http://jsfiddle.net/btbp9o4c/1/
推荐答案
您的错误在这里:
self.newText = 'Added';
需要为:
self.newText('Added');
由于您不正确地重新分配了可观察对象.这是更新的小提琴:
As you are incorrectly reassigning the observable. Here is the updated fiddle:
这篇关于在视图模型内单击时更新输入框的文本-剔除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!