Knockout.js how to access the inner object property on data-bind

后端 未结 5 1963
春和景丽
春和景丽 2021-01-03 19:48

I\'ve a super simple knockout code below:

var data ={
    \"Id\" : 1001,
    \"SalePrice\" : 12345,
    \"ListPrice\" : 333,
    \"ShortDesc\" : \"Tayler 123         


        
5条回答
  •  余生分开走
    2021-01-03 20:22

    Check out the fiddle.All you need to do is this

    
    

    This will populate the span element with sale price defined in the view model.

    From your question it appears to me that what you are trying to do is bind the view model to different elements of the html page. To achieve this you can pass an id parameter to the apply bindings to bind a specific view model to a specific section of the page. Like this

    ko.applyBindings(viewmodel,document.getElementById('id')); 
    

    If you are looking to directly access the view model use the binding context. In this case you need the $data binding context.So the binding in span will change to

    
    

提交回复
热议问题