当前位置: 动力学知识库 > 问答 > 编程问答 >

knockout.js - map json to property on knockout view model

问题描述:

This works fine:

var vm = {};

$.getJSON('file.json', function (data) {

vm.objects = data;

});

ko.applyBindings(vm);

But this doesn't work:

var vm = {

objects: $.getJSON('file.json', function (data) {});

};

ko.applyBindings(vm);

Why not?

Thanks

网友答案:

Because $.getJSON doesn't return anything (or, at least, it doesn't return your data). It's asynchronous, so the only way to access the data is in the callback function.

分享给朋友:
您可能感兴趣的文章:
随机阅读: