【发布时间】:2017-07-19 16:35:36
【问题描述】:
我有一个下拉菜单,允许用户选择某个月份。一个可观察的数组根据用户的选择改变了我的传单地图的层(完美地工作)。
现在我需要选择值myMonth(从零开始的月份数)用于另一个使用内容填充弹出窗口的函数。我只是没有找到在viewModel 函数之外使用变量myMonth 的有效解决方案......任何帮助都非常感谢!
这是我的代码,结果为:popupcontent = "Selected month: undefined"
var myMonth; //global variable myMonth
//oberservable array
function viewModel() {
this.choices = ko.observableArray([
"January","February","March","April","May","June",
"July","August","September","October","November","December"]);
this.selectedChoice = ko.observable();
this.selectedChoice.subscribe(function(newValue) {
myMonth = this.choices.indexOf(this.selectedChoice());
myLayerGroup.clearLayers();
myLayerGroup.addLayer(myLayers[myMonth]);
console.log(myMonth); //works!
return myMonth; // no effect?!
},this);
};
ko.applyBindings(new viewModel());
// popUp window content
function onEachFeature(feature, layer) {
if (feature.properties) {
var popupContent = "Selected month: "+ myMonth;
layer.bindPopup(popupContent);
}
};
【问题讨论】:
标签: javascript arrays knockout.js