我有以下类声明:
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返回该错误。我不知道我的代码中有什么错误。
非常感谢您的帮助。谢谢!