Binding to head elements in KnockoutJS
-
Comments:
- here.
By default, KnockoutJS binds to <body>
, by the look of things. If you have something like:
<html>
<head>
<title data-bind="text: title"></title>
</head>
<body>
...
<script>
var vm = {
title: ko.observable("This is the page title")
};
ko.applyBindings(vm);
</script>
</body>
</html>
The title will not be bound. Instead, you’ll need to use (and I’m using jQuery):
<html>
<head>
<title data-bind="text: title"></title>
</head>
<body>
...
<script>
var vm = {
title: ko.observable("This is the page title")
};
ko.applyBindings(vm, $('html')[0]);
</script>
</body>
</html>
You could also apply the binding twice, once to the head, and once to the body.