我有 ViewModel,它的属性之一是 observableArray(接收器)。我想在添加新记录之前检查集合中是否已经存在电子邮件地址,以确保不会出现重复条目。请帮忙。我尝试了这里的方法如何有条件地将项目推送到可观察数组中?但它不起作用。它无法检测要添加的项目是否已经在列表中。我希望如果调用 AddReceiver 函数,我将验证 newReceiverData 电子邮件地址。这是我的代码。
视图模型
var ReceiversViewModel = function () {
var self = this;
var errorModal = {};
self.firstname = ko.observable();
self.lastname = ko.observable();
self.receivers = ko.observableArray();
self.newReceiver = {
receiverfirstname: ko.observable(''),
receiverlastname: ko.observable(''),
receiveremailaddress: ko.observable('')
};
self.AddReceiver = function () {
var newReceiverData = ko.toJS(self.newReceiver);
if (ReceiverValidate() == true) {
//check if newReceiverData.receiveremailaddress value already exist
self.receivers.push({
EmailAddress: newReceiverData.receiveremailaddress,
FirstName: newReceiverData.receiverfirstname,
LastName: newReceiverData.receiverlastname
});
}
};
};