问题描述
如何在两个不同的视图模型中传递元素的visible属性.假设在一个视图模型中我具有visible是false,在另一个视图模型中的click函数中,我想使该可见成为true.可以使用淘汰赛吗?
How to pass the visible property of an element, in two different view model.Suppose in 1 view model i have visible is false, in another view model in click function i want to make that visible true. Can It be possible Using Knockout.
ViewModel1 = function() {
var self = this;
this.dataItem = ko.observable(false);
};
Viewmodel 2
Viewmodel 2
ViewModel2 = function() {
var self = this;
// Click Function
this.showItem= function(){
ViewModel1.dataItem = ko.observable(true);
};
};
推荐答案
您应该尝试出色的 knockout-postbox .它旨在促进各个视图模型之间的去耦通信.
You should try an excellent knockout-postbox. It is designed to facilitate decoupled communication between separate view models.
在您的情况下,您可以像这样使用它:
In your case you can use it like:
注意: syncWith 用于双向通信,如果要进行单向通信,则应尝试将 subscribeTo 与 publishOn 一起使用>方法.
Note: syncWith used for bidirectional communication, if you want unidirectional communication then you should try subscribeTo with publishOn methods.
Viewmodel 1
ViewModel1 = function() {
var self = this;
this.dataItem = ko.observable(false).syncWith("visible", true);
};
Viewmodel 2
ViewModel2 = function() {
var self = this;
self.dataItem = ko.observable().syncWith("visible", true);
// Click Function
this.showItem= function(){
self.dataItem = ko.observable(true);
};
};
这篇关于两种视图模型之间的敲除传递值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!