无法从HTML文件访问KnockoutJS变量

时间:2016-05-05 07:11:10

标签: javascript html knockout.js data-binding

我的变量patientObj的格式为:patientObj

patientObj已被创建为:

Sent_ID| Receive_ID  |     Time     |     Message
-------+----------+------------------+-----------------
  2    |    1     | 11/21/2015 11:05 | respond final 
-------+----------+------------------+-----------------
  1    |    3     | 11/21/2015 12:16 | Message Final
-------+----------+------------------+-----------------
  4    |    1     | 11/21/2015 12:25 | New message 1
-------+----------+------------------+-----------------

我试图通过动态添加新分区来使用KnockoutJS访问patientObj:

function PAndD(date, ishidden, diagnosis, prescriptionObj)  {
    var self = this;
    self.date = date;
    self.ishidden = ishidden;
    self.diagnosis = diagnosis;
    self.prescriptionsRetrieved = ko.observableArray(prescriptionObj);
};

function Prescription(name, quantity, dosage, type)   {
    var self = this;
    self.name = name;
    self.quantity = quantity;
    self.dosage = dosage;
    self.type = ko.observable(type);
};

function PatientData(name, sex, dob, height, weight, address)   {
    var self = this;
    self.name = name;
    self.sex = sex;
    self.dob = dob;
    self.height = height;
    self.weight = weight;
    self.address = address;
    self.pandd = ko.observableArray([]);
};

function doctorViewModel()    {
    var patientObj = new PatientData(x.name, x.sex, x.dob, x.height, x.weight, x.address);

    for(var i = 0; i < y.length; i++)   {
        var prescriptionObj = [];
        for(var j = 0; j < y[i].prescriptions.length; j++)  {
            prescriptionObj.push(new Prescription(y[i].prescriptions[j].name, y[i].prescriptions[j].quantity, y[i].prescriptions[j].dosage, y[i].prescriptions[j].type));
        }
        patientObj.pandd().push(new PAndD(y[i].date, y[i].ishidden, y[i].diagnosis, prescriptionObj));
    }

    self.patientObj(patientObj);
    console.log("patientList", self.patientObj());
    isPatientDataAvailable.isPatientDataAvailable(true);
}

但是这并没有在HTML中显示任何输出。这里有什么问题?

创建动态元素后的HTML:

var parent_div = document.getElementById('parent_div');
var newdiv = document.createElement('div');
newdiv.setAttribute('id','mydiv');
newdiv.innerHTML = "<p data-bind=\"text: $root.patientObj.name\"></p>";
parent_div.appendChild(newdiv);     

我甚至试着查看数据可用于访问的顺序,结果发现动态部门在加载后试图访问它,所以我不明白问题是什么。一些帮助?

0 个答案:

没有答案