如何在文档的<head>部分创建Angular指令?

Jér*_*nge 9 html javascript html-head angularjs angularjs-directive

我是新来的angular.js.我正在尝试创建一个指令,在<head>html文档部分添加一些标题和元标记,但我遇到了一些麻烦.

我的index.html文件如下:

<!DOCTYPE html>
<html ng-app="myApp">
<head>
    <meta charset="UTF-8">
    <base href="/">
    <seo-title></seo-title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.1/angular.min.js"></script>
    <script src="https://code.angularjs.org/1.4.1/angular-route.min.js"></script>
    <script src="/incl/js/myApp.js"></script>
</head>
<body >
    <div ng-view></div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

我的javascript是:

var app = angular.module ('myApp', ['ngRoute']);

app.config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {

    $routeProvider
        .when('/', { templateUrl: 'routes/home.html'})
        .when('/pageA', { templateUrl: 'routes/pageA.html'})
        .when('/pageB', { templateUrl: 'routes/pageB.html'})
        .otherwise({ redirectTo: '/' });

    $locationProvider.html5Mode({
        enabled: true
    });

}]);

app.directive('seoTitle', function() {
    return {
        restrict: 'E',
        template: '<title>{{seo.title}}</title>'
    };
});
Run Code Online (Sandbox Code Playgroud)

当我打开检查器时,指令已被移动到<body>并且未被模板替换:

在此输入图像描述

如何在标题中创建指令?

PS:代码示例会很棒!

Mar*_*tin 5

您的指令不需要输入head来设置标题。只要让您的指令注入$window和设置$window.document.title = 'your title'

UPDATE这是更新元标记的方法。

为了更新元标记,我将使用以下指令:

mmMetaTags.$inject = ['metaTags'];
function mmMetaTags(metaTags) {

    return {
        restrict: 'A',
        link: function(scope, element) {

            metaTags.metaTags.forEach(function(tag) {
                addMetaTag(tag.name, tag.content)
            });

            metaTags.subscribe(addMetaTag);

            function addMetaTag(name, content) {

                var tag = element[0].querySelector('meta[name="' + name + '"]'); 

                if (tag) {

                    tag.setAttribute('content', content);
                } else {

                    element.append('<meta name="' + name + '" content="' + content + '">');
                }
            }
        }
    }

}

directive('mmMetaTags', mmMetaTags);
Run Code Online (Sandbox Code Playgroud)

连同设置metaTag的服务:

function MetaTags() {

    // private
    this._tags = [];

    // private
    this._subscriber;

    var self = this;
    Object.defineProperty(this, 'metaTags', { get: function() {
        return self._tags;
     }});
}

MetaTags.prototype.addMetaTag = function(name, content) {
    this._tags.push({ name: name, content: content });
    this._updateSubscriber(name, content);
}

MetaTags.prototype.subscribe = function(callback) {
    if (!this.subscriber) {
        this._subscriber = callback;
    } else {
        throw new Error('Subscriber already attached. Only one subscriber may be added as there can only be one instance of <head>');
    }
}

// private
MetaTags.prototype._updateSubscriber = function(name, content) {
    this.subscriber(name, content);    
}

service('metaTags', MetaTags);
Run Code Online (Sandbox Code Playgroud)

因此,您head可以在标记中包含属性mm-meta-tags。然后,在控制器中,您将注入metaTags服务并调用addMetaTag以更新标签。


Jum*_*nco 2

您的答案在这里:使用 UI-Router 设置页面标题,在您的代码中实现它可能是:

<!DOCTYPE html>
<html ng-app="myApp">
<head>
    <meta charset="UTF-8">
    <base href="/">
    <title seo-title>doesn't work</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.1/angular.min.js"></script>
    <script src="https://code.angularjs.org/1.4.1/angular-route.min.js"></script>
    <script src="/incl/js/myApp.js"></script>
</head>
<body >
    <div ng-view></div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

而你呢:

app.directive('seoTitle', function() {
return {
    restrict: 'a',
    template: 'works'
};
Run Code Online (Sandbox Code Playgroud)

你只需要添加一个控制器或一些逻辑来设置你想要的标题