大家好我是knockout.js的新手,并且感兴趣的是将数据从服务器绑定到淘汰赛的最佳方法。例如,我想绑定类别变量,然后在选择中使用:
var myViewModel = function(){
var self = this
self.categories = []
$.getJson("/ajax/categories", function(r){
self.categories = r;
}
}
ko.applyBindings(new myViewModel ());
// in html
<select data-bind='options: categories'> </select>
答案 0 :(得分:1)
var myViewModel = function() {
var self = this
self.categories = ko.observableArray([]);
$.getJson("/ajax/categories", function(r) {
self.categories(r);
}
}
ko.applyBindings(new myViewModel());
in html
<select data-bind='options: categories'> </select>
答案 1 :(得分:0)
上面的解释似乎很好,但在2个地方的语法错误很少:
1. $ .getJson应为$.getJSON
(大写)
2. $.getJSON
关闭括号缺少
其他一切都很好,可以从下面复制更正后的代码:
var myViewModel = function() {
var self = this
self.categories = ko.observableArray([]);
$.getJSON("/ajax/categories", function(r) {
self.categories(r);
})
}
ko.applyBindings(new myViewModel());