我经历了很多淘汰文章,但是我没有将json对象映射到淘汰视图模型
{
"VehicleModels":[
{
"Name":"Model 1",
"Model":{
"MakeName":"Ford"
},
"Styles":[
{
"StockImage":"http://google.com"
}
]
},
{
"Name":"Model 2",
"Model":{
"MakeName":"Ford"
},
"Styles":[
{
"StockImage":"http://bing.com"
}
]
}
]
}
这是我的jsfiddle代码jsfiddle Link
答案 0 :(得分:1)
好的,我已经更新了小提琴:
本质:
var mapping = {
'Styles': {
create: function (options) { // I fiddled with this, play around with it
var self = options.data;
self.stockimage = ko.observable();
return self;
}
}
};
var data = {"VehicleModels":[{"Name":"Model 1","Model":{"MakeName":"Ford"},"Styles":[{"StockImage":"http://google.com"}]},{"Name":"Model 2","Model":{"MakeName":"Ford"},"Styles":[{"StockImage":"http://bing.com"}]}]};
var viewModel = ko.mapping.fromJS(data,mapping); // Here you did not need to put this as the 3rd parameter
ko.applyBindings(viewModel); // you need to apply the bindings at some point
最后,您已尝试为范围指定值:
<span data-bind='value: Name' />
Spans没有价值我认为你想要文字:
<span data-bind='text: Name' />