从脚本标记

时间:2017-02-10 16:49:06

标签: angularjs

更新:这是"X Y problem"。我尝试这样做的方式是in this answer回答了一个完全重复的问题。但是,我最好使用ng-bind达到我的实际目标,而不是手工重新构建一个kludgey伪单向绑定机制。

-

在下面的代码中,controller回调中未定义ready()。我真的不这么认为。

这里的目的是让控制器在其标题发生变化(简单)时“引发事件”,并在发生时更新document.title。问题是,HTML是否有任何方法可以让控制器完成这项工作?

<html 
      ng-app="foo" 
      ng-controller="FooController as controller"
      >
<head>
    <title>Default Title</title>
    <script src="http://code.angularjs.org/snapshot/angular.min.js"></script>

    <script src="foo.js"></script>

    <script type="text/javascript">
        angular.element(window.document.body).ready(
            function () {
                //  This does get called
                //alert('test');

                //  controller isn't defined, not too surprisingly. 
                controller.titleChanged = function (title) {
                    //alert(title)
                    document.title = title;
                };
            });
    </script>
</head>

我可以像这样设置标题:

<title>{{controller.title}}</title>

...但是我在浏览器历史记录中看到{{controller.title}}而不是有意义的标题,而且我理解它也是搜索引擎会看到的内容。

当然我可以让控制器将新标题分配给document.title,而现在这就是我正在做的事情,但我更喜欢让控制器不知道观点。

1 个答案:

答案 0 :(得分:1)

而不是插值使用ng-bind指令。