我在訂閱HTML頁面中的下拉值更改時遇到實現淘汰Js的問題。這是我的HTML無法獲取未定義或空引用的屬性「訂閱」
<div>
<span>Request Types : </span>
<select data-bind="options: requestTypes,
optionsText: 'Name',
optionsCaption: '--Select--',
value: selectedRequestType">
</select>
</div>
雖然如下圖所示
wd.viewModel = function() {
var data = {};
var self = this;
var url = '';
var selectedRequestingCountry = ko.observable("");
self.optionsProvider = new wd.optionsProvider(self);
self.objGenralInfo = new wd.GI_form(data);
/* For Requesting Country */
url = wd.ConfigObject.baseUrl + wd.ConfigObject.masterServiceUrl + wd.ConfigObject.category.importingCountries; // Fetch Requesting Countries
self.importingCountries = self.optionsProvider.get("importingCountries", url, {});
self.selectedRequestingCountry = ko.observable("");
this.requestingCountryArea = ko.observable("");
/* For Generic Name*/
url = wd.ConfigObject.baseUrl + wd.ConfigObject.masterServiceUrl + wd.ConfigObject.category.genericNames; // Fetch Generic Names
self.genericNames = self.optionsProvider.get("genericNames", url, {});
self.selectedGenericName = ko.observable("");
/* Override Requesting Country Address */
self.IsOverride = ko.observable(false);
/* For Request Types */
self.selectedRequestType = ko.observable("");
self.selectedTradeName = ko.observable("");
};
// Whenever the selected Generic Name changes, populate the Request Type
var myName = wd.viewModel.selectedGenericName.subscribe(function() {
wd.viewModel.url = wd.ConfigObject.baseUrl + wd.ConfigObject.masterServiceUrl + wd.ConfigObject.category.requestTypes + "/" +
wd.viewModel.selectedGenericName().Name;
wd.viewModel.requestTypes = wd.viewModel.optionsProvider.get("requestTypes", url, ''); // Fetch request types based on generic name
}, wd.viewModel);
$(function() {
ko.applyBindings(wd.viewModel);
});
請求類型下拉值視圖模型後才通用名稱下拉顯示值填充是loaded.User必須首先選擇通用名稱。在頁面加載時,它會引發錯誤「無法獲取屬性」訂閱「未定義或空引用」。
任何幫助是非常讚賞,因爲我是新的淘汰JavaScript的MVVM框架。
看起來像wd.viewModel.selectedGenericName is not defined when you try to initialize myName。'ko.observable(「」)''的期望值是多少? – Bonsaigin
是wd.viewModel.selectedGenericName在頁面加載時未定義,如下所示HTML代碼(
ko.observable(「」)的值是一個可觀察的函數(即,函數observable(){if(arguments.length> 0){//寫//如果值未改變,則忽略寫入if(observable.isDifferent(_latestValue,arguments [0])){observable.valueWillMutate();) –