如何使用angular.js绑定title元素中的数据 [英] How to bind data in title element using angular.js

查看:87
本文介绍了如何使用angular.js绑定title元素中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Angular.js并设置< title> {{title}}< / title> ,我尝试使用select元素<更改

I'm learning Angular.js and I set <title>{{title}}</title> and I try to change that using select element

<select ng-model="user.title">
    <option value="Lorem">Lorem</option>
    <option value="Ipsum">Ipsum</option>
    <option value="Dolor">Dolor</option>
</select>

我尝试 ng-change ng-select with set()

I try ng-change and ng-select with set()

function ctrl($scope) {
    $scope.title = 'hello'; // this set the title
    $scope.set = function() {
        $scope.title = $scope.user.title; // this not
    };
}

这个功能不起作用,但是当我没有设置时它可以正常工作一个函数。

THe function don't work, but it work when I just set it without a function.

我也尝试创建更改指令:

I also try to create change directive:

app.directive('change', function() {
    console.log('change');
    return {
        link: function(scope, element, attrs) {
            console.log(arguments);
            element[0].onChange = function() {
                scope[attrs[0]]();
            };
        }
    };
});

但这也不起作用。 Console.log根本不执行。

but this too don't work. Console.log don't execute at all.

推荐答案

如果没有额外的代码,一切都应该正常工作:

Everything should work fine without extra code:

<html data-ng-app="app">
  <head>
    <title>{{title}}</title>
  </head>
  <body>
    {{title}}
    <select ng-model="title">
      <option value="Lorem">Lorem</option>
      <option value="Ipsum">Ipsum</option>
      <option value="Dolor">Dolor</option>
    </select>
  </body>
</html>

这就是全部。 这是一支笔。

这篇关于如何使用angular.js绑定title元素中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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