有没有办法使用 Knockout.js 通过数据绑定来设置页面标题? [英] Is there a way to set the page title by data-binding using Knockout.js?

查看:8
本文介绍了有没有办法使用 Knockout.js 通过数据绑定来设置页面标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 Title 属性的 viewModel.我想使用该属性设置页面标题.这是我已经尝试过的,但没有用:

I have a viewModel with a Title property. I'd like to set the page title using that property. Here's what I tried already, which didn't work:

<html>
   <head>
   <title data-bind="text: Title"></title>
</head>
<body>
   <span data-bind="text: Title"/> <!-- this displays the title properly -->
</body>

浏览器标题为空白/默认,而不是我的 Title 属性的值.

The browser title is blank/default instead of the value of my Title property.

推荐答案

尝试给你的 html 元素一个 id

Try giving your html element an id

<html id="htmlTop" xmlns="http://www.w3.org/1999/xhtml">

并将您的 viewModel 应用到它

and applying your viewModel to it

ko.applyBindings(viewModel, document.getElementById("htmlTop"));

编辑

这对我有用;我刚刚运行了这个页面,标题是你好".仔细检查您的代码是否有拼写错误.

This works for me; I just ran this page and the title said "Hello". Double check your code for typos.

<html id="htmlTop">

    <head>
      <title data-bind="text: title"></title>

      <script type='text/javascript' src='jquery.min.js'></script>
      <script type='text/javascript' src='knockout-1.2.1.js'></script>

      <script type="text/javascript">

          $(function () {
              var viewModel = { title: "Hello" };
              ko.applyBindings(viewModel, document.getElementById("htmlTop"));
          });

      </script>

    </head>

    <body>
    </body>
</html>

截图:

这篇关于有没有办法使用 Knockout.js 通过数据绑定来设置页面标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆