簡單的方式,使用 knockout.mapping.js。html
一、引入knockout.mapping.js。app
二、聲明模型spa
var model = { task:null, feedbacks:[], feedbackPhotos:null }
三、mapping 綁定 code
var viewModel = ko.mapping.fromJS(model);
四、聲明綁定orm
$(function() {
ko.applyBindings(viewModel);
initData();
})
五、賦值htm
viewModel.task(result.data);
六、使用blog
<div id="form" data-bind="with:task">
<div id="form-head">
<div id="form-head-inner">
<div id="form-title" class="horizontal">
<span data-bind="text:taskName+'【'+taskId+'】'"></span>
</div>
<div id="form-action">
</div>
</div>
</div>
<div class="container" data-bind="foreach:feedbacks"> <div class="row"> <div class="col-md-12 col-sm-12"> <div class="section-title"> <h1 class="heading bold" data-bind="html:moment(feedbackPlanTime).format('YYYY-MM-DD')"></h1> </div> </div> </div>
注意綁定方法的時候get
<td class="order-info-val text-right" data-bind="text:$root.getStat(custDec015)"></td>
var viewModel = {orderInfo:ko.observable(), getStat:function(phase){ // console.log("========"+phase()); return getStat(phase())//綁定此處是個方法,須要加() }};