为什么 observable 不通知 if block in knockout?
why is observable not notifying if block in knockout?
如果在选择更改时未通知绑定,则敲除。选择更改时,如果所选国家/地区 'eu' 设置为“1”,我希望发生一些事情。
<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 -->
'countries'(示例)如下。
{
"countries": [
{
"country_code": "BD",
"country_name": "Bangladesh",
"eu": "0"
},
{
"country_code": "BE",
"country_name": "Belgium",
"eu": "1"
}
]
}
更新
我正在使用 CoffeeScript
@selectedCountry = ko.observable()
@countries = ko.observableArray []
它对我有用fiddle。不确定我的代码和你的有什么区别,因为我们看不到你 JavaScript.
https://jsfiddle.net/xggu9Lv2/7/
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 -->
如果在选择更改时未通知绑定,则敲除。选择更改时,如果所选国家/地区 'eu' 设置为“1”,我希望发生一些事情。
<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 -->
'countries'(示例)如下。
{
"countries": [
{
"country_code": "BD",
"country_name": "Bangladesh",
"eu": "0"
},
{
"country_code": "BE",
"country_name": "Belgium",
"eu": "1"
}
]
}
更新
我正在使用 CoffeeScript
@selectedCountry = ko.observable()
@countries = ko.observableArray []
它对我有用fiddle。不确定我的代码和你的有什么区别,因为我们看不到你 JavaScript.
https://jsfiddle.net/xggu9Lv2/7/
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 -->