0
我試圖循環瀏覽集合中的項目列表,如果要在集合上添加的項目的電子郵件地址已經存在,但我得到運行時錯誤,請比較每個項目「JavaScript運行時錯誤:預計的功能「。請任何人都可以幫助我下面我的代碼丟失什麼?Knockout.js運行時錯誤
視圖模型
self.AddReceiver = function() {
var newReceiverData = ko.toJS(self.newReceiver);
if (ReceiverValidate()) {
ko.utils.arrayForEach(self.receivers(), function (item) {
if (item.EmailAddress() == newReceiverData.receiveremailaddress) {
alert("Email already exist on the list");
return false;
}
});
self.receivers.push({
EmailAddress: newReceiverData.receiveremailaddress,
FirstName: newReceiverData.receiverfirstname,
LastName: newReceiverData.receiverlastname
});
ClearReceiverInput();
}
};
截圖錯誤
你確定'item.EmailAddress()'是一個可觀察的嗎?用'if(ko.utils.unwrapObservable(item.EmailAddress)== newReceiverData.receiveremailaddress)'' – nemesv
'作爲旁註如果你返回false,'ko.utils.arrayForEach'不會停止;'它會遍歷所有數組中的元素無論如何。所以應該考慮使用'ko.utils.arrayFirst' isntead ... – nemesv
@nemesv是它不可觀察,但它是self.receivers = ko.observableArray();的屬性。 –