我使用 jquery.load 函数动态加载页面,但加载的页面未绑定到 viewModel?
应用程序.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());
索引.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>