简体   繁体   English

正确使用data-bind visible属性

[英]Properly using the data-bind visible property

I have a view that displays data from a foreach loop in different categories depending on the type. 我有一个视图,根据类型显示不同类别的foreach循环数据。 Each category will contain a number of users - I created an object that will check to see if the number of users in a category are more than 10 then the text for the visible bind will show. 每个类别将包含许多用户 - 我创建了一个对象,该对象将检查一个类别中的用户数是否超过10,然后将显示可见绑定的文本。 And for the category that doesn't have more than 10 it will not show the text. 对于不超过10的类别,它不会显示文本。

My question: if the first category doesn't have 10 it won't show text does that mean that it won't also show text for the remaining categories? 我的问题:如果第一个类别没有10,那么它不显示文本是否意味着它不会显示其余类别的文本?

Help with : the visible binding is not working even though a category would contain more than 10 and not sure why. 帮助 :即使类别包含10个以上并且不确定原因,可见绑定也不起作用。

Here is my JSFiddle: http://jsfiddle.net/xNdJk/1/ 这是我的JSFiddle: http//jsfiddle.net/xNdJk/1/

JavaScript: JavaScript的:

var userViewModel = function (data) {
        var _self = this;
        _self.Name = ko.observable(data.Name);
        _self.Letter = ko.observable(data.Letter);
        _self.ShowLetter = ko.computed(function () {
            return (roleViewModel.UserCount > 13);
        });
    };

var typeViewModel = function (data) {
        var _self = this;
        _self.ContentType = ko.observable(data.ContentType);
        _self.Name = ko.observable(data.Name);
        _self.Rank = ko.observable(data.Rank);
        _self.UserCount = ko.observable(data.UserCount);
        _self.Users = ko.observableArray([]);
    };

View: 视图:

<div class="collapse in" data-bind="template: { name: 'list', foreach: $data.Users }">

</div>
<div id="letter" data-bind="visible:ShowLetter, text: Letter"></div>

You are mixing classes and instances, you have created a secondModel class but you never instance it, here is a working example 您正在混合类和实例,您已经创建了一个secondModel类,但是您从未实例化它,这是一个工作示例

http://jsfiddle.net/xNdJk/2/ http://jsfiddle.net/xNdJk/2/

var viewModel = function(){        
    this.Letter = ko.observable('Hello, World!');
    this.secondModel = new secondModel();

    this.shouldShowMessage = ko.computed(function() {
        return (this.secondModel.UserCount() > 13);
    }, this);
}

var secondModel = function(){
    var self = this;
    self.UserCount = ko.observable(153);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM