未捕获的TypeError:无法读取未定义的属性'addNewDevice'

时间:2019-06-12 07:48:53

标签: javascript

我有以下类声明:

class DevicesModel{
    constructor(){
        //khoi tao devices 
        this.devices = [];
        this.tt = 1;
    }
    addNewDevice(ma,ten,dvt,trongLuong){
        const device = {tt: this.tt,ma,ten,dvt,trongLuong};
        this.tt += 1;
        this.devices.push(device);
        return device;
    }
}
class DevicesController{
    constructor(){
        this.devicesModel = new DevicesModel();
        this.devicesView = new DevicesView();
    }
    addNewDevice(){
        const maSo = document.getElementById("ma-sp").value;
        const ten = document.getElementById("ten-sp").value;
        const dvt = document.getElementById("dvt").value;
        const trongLuong = document.getElementById("trong-luong").value;
        if (maSo === '' || ten === '' || dvt === '' || trongLuong === '') {
            return;
        }
        //error is here
        this.devicesModel.addNewDevice(maSo,ten,dvt,trongLuong);
        this.devicesView.addNewDevice(maSo,ten,dvt,trongLuong);
        this.tt += 1;
        //dong modal 
        document.querySelector('.bg-modal').style.display = "none";
    }
    start(){
        this.devicesView.showHideModal();
        document.getElementById("save").addEventListener('click',this.addNewDevice);
    }
}
const devicesController = new DevicesController();
devicesController.start();

当我尝试添加新设备时,this.devicesModel返回该错误。我不知道我的代码中有什么错误。

非常感谢您的帮助。谢谢!

0 个答案:

没有答案