2014-09-02 66 views
0

如果數組長度大於0,我想隱藏div。通過放置警告消息,我確認數組長度= 0,然後div應該隱藏。 但是,當運行應用程序的div可見KnockOut data-bind =「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> 

回答

1
<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> 

您需要與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>