javascript - How to pass a constant outside of a viewmodel to a Knockout binding? -
given there enum , viewmodel:
var myenum = { val1 = 1, val2 = 2 }; var someviewmodel = {}; ko.applybindings(someviewmodel);
and view:
<div data-bind="template: 'my-template', data: myenum.val1"></div>
i can't find way pass value of myenum.val1
binding, because knockout looks inside viewmodel. prepending $root.myenum.val1
doesn't work either, still looks within viewmodel.
any ideas how working?
i prefer declare viewmodel function first instead of making them object directly. doing can reuse in future if needed , have neater process pass object viewmodel during initialization.
here example:
function someviewmodel(enum) { this.myenum = enum; }; var myenum = { val1: 1, val2: 2 }; ko.applybindings(new someviewmodel(myenum));
then in html can following
<div data-bind="template: 'my-template', data: myenum.val1"></div>
Comments
Post a Comment