【发布时间】:2014-07-08 05:41:21
【问题描述】:
我在这篇文章中遇到了类似的问题:[问题]:Can't get the most simple knockout.js sample to work?
这是一个带有一个标题的简单新闻模型,我想将它显示在一个 html span 标签中。我能够做一个 js 警报,它给出了正确的值,但是 span 标签不显示该值。
将 applyBindings 放入 onload 调用的答案不起作用。它最初是在文档准备功能中。
以下是代码示例:
HTML:
Title: <span data-bind="text: Title"> </span>
JS:
function NewsItem(data) {
this.NewsItemId = ko.observable(data.NewsItemId);
this.Title = ko.observable(data.Title);
}
function NewsItemViewModel() {
var self = this;
self.NewsItemId = ko.observable();
self.Title = ko.observable();
$.ajax({
type: "POST",
url: "GetNewsItemById",
data: "{idstring:'1'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (results) {
self.Title = results.d.Title;
alert(self.Title);
},
error: function (err) {
alert(err.status + " - " + err.statusText);
}
});
}
$(document).ready(function () {
ko.applyBindings(new NewsItemViewModel());
});
感谢您的帮助!
【问题讨论】:
标签: knockout.js