0
self.work_days = ko.observableArray();
self.work_days().push(new WorkDayVM({}, new_date))//new_date is the date supplied from the form
function WorkDayVM(data, day) {
var self = this;
self.in_time1 = ko.observable();
self.out_time1 = ko.observable();
self.in_time2 = ko.observable();
self.out_time2 = ko.observable();
self.work_time = ko.computed(function() {
var in_time1_raw = self.in_time1();
var out_time1_raw = self.out_time1();
var in_time2_raw = self.in_time2();
var out_time2_raw = self.out_time2();
if(!in_time1_raw || !out_time1_raw || !in_time2_raw || !out_time2_raw)
return;
var t1 = get_minutes(in_time1_raw);
var t2 = get_minutes(out_time1_raw);
var t3 = get_minutes(in_time2_raw);
var t4 = get_minutes(out_time2_raw);
res = t2 - t1 + t4 - t3;
return get_hr_m(res);//returns hr:min
}, this);
}
console.log(self.work_days()[0].work_time); //prints dependentobservable()
console.log(self.work_days()[0].work_time());//prints undefined
我想獲取work_time值。如何訪問該值?如何訪問ko.computed在knockout js中觀察數組的方法
是否有任何數據被推入'WorkDayVM'?在你的例子中,向你的'WorkDayVM'推入一個空的'data'對象會導致計算出的'work_time'返回一個未定義的。 – rwisch45