我使用jquery.load函数动态加载页面,但加载的页面没有绑定到viewModel?
app.js
function viewModel(){
var self = this;
self.users = ko.observable();
Sammy(function() {
this.get("#/users",function() {
$.get("/api/users",function(data){
self.users(data);
});
$("#content").load("pages/users.html");
});
}).run("#/");
}
ko.applyBindings(new viewModel());
的index.html:
<html>
<body>
<div id="content"></div>
<script src="statics/js/lib/jquery-1.8.2.js"></script>
<script src="statics/js/lib/knockout-2.2.0.debug.js"></script>
<script src="statics/js/lib/knockout.mapping.js"></script>
<script src="statics/js/lib/sammy.js"></script>
<script src="statics/js/lib/app.js"></script>
</body>
</html>
页/ users.html
<ul data-bind="foreach: users">
<li><span data-bind="text: fullName"></span></li>
</ul>
答案 0 :(得分:11)
$("#content").load("/pages/users.html", function () {
ko.applyBindings(new viewModel());
}
应该这样做......
答案 1 :(得分:-1)
我在动态页面加载后解决了我的问题重新应用绑定
function viewModel(){
var self = this;
self.users = ko.observable();
Sammy(function() {
this.get("#/users",function() {
$.get("/api/users",function(data){
self.users(data);
});
$("#content").load("pages/users.html", null, function (response, status, xhr) {
if (status != "error") {
}
ko.applyBindings(self, $(".dynamic-page-content").get(0));
}
});
});
}).run("#/");
}
ko.applyBindings(new viewModel());