简体   繁体   中英

KnockOut data-bind="visible: Need help to troubleshhot the issue

I want to hide the div if array length is >0. By putting an alert message I confirmed array length is = 0 and then div should be hidden. But when run the app div is visible.

<body>
    <form id="form1" runat="server">
    <div>

    <div data-bind="visible: myValues().length > 0">
    You will see this message only when 'myValues' has at least one member.
</div>


    </div>
    </form>
</body>
<script type="text/javascript">
    var viewModel = {
        myValues: ko.observableArray([]) // Initially empty, so message hidden
    };
    alert('The length of the array is ' + viewModel.myValues().length);
</script>
<body>
    <form id="form1" runat="server">
    <div>

    <div data-bind="visible: myValues().length > 0">
    You will see this message only when 'myValues' has at least one member.
   </div>

You need to bind this with applybindings:

    </div>
    </form>
</body>
<script type="text/javascript">
    var viewModel = {
        myValues: ko.observableArray([]) // Initially empty, so message hidden
    };
    alert('The length of the array is ' + viewModel.myValues().length);



    ko.applyBindings(viewModel); 
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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