Search code examples
knockout.jsknockout-3.0

data-bind with multiple attributes and null check?


I m binding data using this code:

<a class="btnAttach" href="#" data-bind="attr:{'data-mmid':mm_id}" >

My data object is:

{
  "mm_id": 1
  account: {
    "accId": "Acc1"
    "accName": "Az Corp"
  }
}

I need to modify binding. Currently code binds mm_id as an "data-mmid" attribute. But I need to add another attribute accId as separate data-accId attribute. Probably i can't have 2 data-bind. Please can you guide how to do this ? Another catch is account object can be null too. So further complaxity is how to do null check and create 2 attributes.

I highly appreciate your help and time.


Solution

  • <div data-bind="if: account">
      account: <a data-bind="attr:{'data-mmid':mm_id,'data-accId':account.accId}"> </a> </div>
    

    please check the above code.