模板中的 ObservableArray 在创建后未应用可见绑定
无论出于何种原因,每当 GroupType
选择列表发生变化时,我都无法让淘汰赛触发可见更新。我有一个简化的示例如下:
<script id="process" type="text/x-jquery-tmpl">
<select data-bind="value: GroupType">
<option value="0">example 0</option>
<option value="1">example 1</option>
<option value="2">example 2</option>
<option value="3">example 3</option>
</select>
<select data-bind="value: Group, visible: GroupType() == '2'">
<option value="1">option 1</option>
<option value="2">option 2</option>
</select>
<input type="text" data-bind="value: Group, visible: GroupType() != '2'"/>
</script>
<div data-bind="template { name: 'process', foreach: Processes }"></div>
<script type="text/javascript">
var viewModel = {
Processes: ko.observableArray([
{ GroupType: ko.observable("1"), Group: ko.observable("Blah") },
{ GroupType: ko.observable("2"), Group: ko.observable("1") }
])
};
ko.applyBindings(viewModel);
</script>
编辑 1: 我已经包含了下面建议的更改,但它不允许这两个项目切换,而是只显示最后一个项目,而不管所选的 GroupType
编辑2:我为此制作了一个jsFiddle:http://jsfiddle.net/2jjgH/
编辑 3(最终):上面已进行编辑以反映下面的答案。
For whatever reason, I cannot get knockout to trigger an update on visible whenever the GroupType
select list changes. I have a cut-down example below:
<script id="process" type="text/x-jquery-tmpl">
<select data-bind="value: GroupType">
<option value="0">example 0</option>
<option value="1">example 1</option>
<option value="2">example 2</option>
<option value="3">example 3</option>
</select>
<select data-bind="value: Group, visible: GroupType() == '2'">
<option value="1">option 1</option>
<option value="2">option 2</option>
</select>
<input type="text" data-bind="value: Group, visible: GroupType() != '2'"/>
</script>
<div data-bind="template { name: 'process', foreach: Processes }"></div>
<script type="text/javascript">
var viewModel = {
Processes: ko.observableArray([
{ GroupType: ko.observable("1"), Group: ko.observable("Blah") },
{ GroupType: ko.observable("2"), Group: ko.observable("1") }
])
};
ko.applyBindings(viewModel);
</script>
EDIT 1: I have included the changes suggested below, but instead of allowing the two items to switch, it just shows the last one regardless of the selected GroupType
EDIT 2: I have made a jsFiddle for this: http://jsfiddle.net/2jjgH/
EDIT 3 (Final): Edit has been made above to reflect answer below.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
observableArrays 仅通知数组本身的更改(添加项目、删除项目、替换整个数组)。他们不跟踪个人财产。您需要创建
GroupType
和Group
observables
才能在您的场景中更新 UI。observableArrays
only notify on changes to the array itself (add items, remove items, replace the entire array). They do not track the individual properties. You would need to makeGroupType
andGroup
observables
to have the UI updated in your scenario.