Knockout.js 为什么observable不通知block in knockout?
选择更改时,不会通知剔除if绑定。当选择更改时,如果所选国家的“eu”设置为“1”,我希望发生一些事情Knockout.js 为什么observable不通知block in knockout?,knockout.js,coffeescript,Knockout.js,Coffeescript,选择更改时,不会通知剔除if绑定。当选择更改时,如果所选国家的“eu”设置为“1”,我希望发生一些事情 <select data-bind="options: countries, optionsText: function(item) { return item.country_name }, value:selectedCountry"> </select> <!-- These *ARE* all updated when c
<select data-bind="options: countries,
optionsText: function(item) {
return item.country_name
},
value:selectedCountry">
</select>
<!-- These *ARE* all updated when country selection changes -->
<div data-bind="visible: selectedCountry()">
<span data-bind="text: selectedCountry().eu"></span>
<span data-bind="text: selectedCountry().country_name"></span>
<span data-bind="text: selectedCountry().country_code"></span>
</div>
<!-- This is *NOT* updated -->
<!-- ko if: selectedCountry().eu === "1" -->
<span>You selected country from EU
</span>
<!-- /ko -->
更新
我在用咖啡脚本
@selectedCountry = ko.observable()
@countries = ko.observableArray []
这把小提琴对我很管用。不确定我的代码和你的代码有什么区别,因为我们看不到你的JavaScript
var国家=[
{
“国家/地区代码”:“A2”,
“国家/地区名称”:“卫星提供商”,
“欧盟”:“0”
},
{
“国家/地区代码”:“A2”,
“国家/地区名称”:“卫星提供商”,
“欧盟”:“1”
}
]
var ViewModel=函数(){
var self=这个;
self.countries=ko.observearray(国家);
self.selectedCountry=ko.可观察(空);
};
应用绑定(新的ViewModel());
您选择了欧盟国家
本身是可观察的
?可能是因为“eu:“0”
不等于1
?@RichardMacarthy,当您在
下拉列表中选择一个选项时,它将等于1。@RichardMacarthy可能是,但这取决于选择-更新的问题是否更清楚它似乎有效。看,我没有在咖啡脚本中将国家正确地声明为可观察的一天
@selectedCountry = ko.observable()
@countries = ko.observableArray []
var countries = [
{
"country_code": "A2",
"country_name": "Satellite Provider",
"eu": "0"
},
{
"country_code": "A2",
"country_name": "Satellite Provider",
"eu": "1"
}
]
var ViewModel = function() {
var self = this;
self.countries = ko.observableArray(countries);
self.selectedCountry = ko.observable(null);
};
ko.applyBindings(new ViewModel());
<select data-bind="options: countries,
optionsText: function(item) {
return item.country_name
},
value:selectedCountry">
</select>
<div data-bind="visible: selectedCountry()">
<span data-bind="text: selectedCountry().eu"></span>
<span data-bind="text: selectedCountry().country_name"></span>
<span data-bind="text: selectedCountry().country_code"></span>
</div>
<!-- ko if: selectedCountry().eu === "1" -->
<span>You selected country from EU
</span>
<!-- /ko -->