如何在AngularJS中更新元标记?

问题描述 投票:39回答:5

我正在使用AngularJS开发应用程序。我想在路线变更时更新中继标记。如何更新AngularJS中的元标记,该标记可以显示在页面的“查看源代码”中?

这里是HTML代码-

  <!DOCTYPE html>
    <html ng-app="app">
        <head>
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <meta name="fragment" content="!" />
            <meta name="title" content="Test App">
            <meta name="description" content="Test App">
            <meta name="keywords" content="Test,App">

            <link rel="stylesheet" href="css/jquery-ui-1.10.2.custom.min.css" />
            <link rel="stylesheet" href="css/extra.css" />
            <script src="js/libs/jquery-1.8.3.min.js"></script>
            <script src="js/libs/jquery-ui-1.10.2.custom.min.js"></script>
            <script src="js/libs/angular.min.js"></script>
            <script src="js/controller.js"></script>
            <script src="js/routes.js"></script>
        </head>
        <body>
            <div ng-controller="mainCtrl" class="main-container" loading>
                <div class="container-holder">
                    <div class="container">
                        <div ng-include src='"elements/header.html"'></div>
                        <div ng-view class="clearfix"></div>
                    </div>
                </div>

                <div ng-controller="userCtrl" id="test">
                    <div class="container" class="login-container">
                        <div id="login-logo">
                            <img src="images/logo-300.png" alt="" class="login-img"/>
                            <br />
                            <div ng-view></div>
                        </div>
                    </div>
                </div>
        </body>
    </html>
angularjs meta-tags
5个回答
36
投票
<html ng-app="app">
    <title ng-bind="metaservice.metaTitle()">Test</title>
    <meta name="description" content="{{ metaservice.metaDescription() }}" />
    <meta name="keywords" content="{{ metaservice.metaKeywords() }}" />


<script>
    var app = angular.module('app',[]);
    app.service('MetaService', function() {
       var title = 'Web App';
       var metaDescription = '';
       var metaKeywords = '';
       return {
          set: function(newTitle, newMetaDescription, newKeywords) {
              metaKeywords = newKeywords;
              metaDescription = newMetaDescription;
              title = newTitle; 
          },
          metaTitle: function(){ return title; },
          metaDescription: function() { return metaDescription; },
          metaKeywords: function() { return metaKeywords; }
       }
    });

   app.controller('myCtrl',function($scope,$rootScope,MetaService){
      $rootScope.metaservice = MetaService;
      $rootScope.metaservice.set("Web App","desc","blah blah");
   });
</script>
 <body ng-controller="myCtrl"></body>


</html>

8
投票

如果使用angular-ui-router,则可以使用ui-router-metatags


7
投票

大约两天前,我通过创建服务并使用$ window和一些经典的javascript解决了这个问题。

在您的html标记中创建所需的任何元标记...(请随时将它们保留为空白,或者您可以将它们设置为默认值。)

<head> 
    <meta name="title"/>
    <meta name="description"/>
</head>

然后,我们需要像这样创建服务。

angular.module('app').service('MetadataService', ['$window', function($window){
 var self = this;
 self.setMetaTags = function (tagData){
   $window.document.getElementsByName('title')[0].content = tagData.title;
   $window.document.getElementsByName('description')[0].content = tagData.description;
 }; 
}]);

现在,我们需要在控制器内使用“ self.setMetaTags”服务(在初始化时...您只需在控制器上的任何位置调用该函数。

  angular.module('app').controller('TestController', ['MetadataService',function(MetadataService){

   MetadataService.setMetaTags({
       title: 'this',
       description: 'works'
    });

   }]);

1
投票

[在大多数浏览器中执行“查看源代码”时,您会看到在对DOM进行任何JavaScript操作之前最初从服务器发送的文档。 AngularJS应用程序通常会执行很多DOM操作,但它实际上从未更改原始文档。当您执行诸如右键单击->检查FireFox或Chrome(使用开发工具)中的元素时,您将看到包含所有JavaScript更新的呈现的DOM。

因此,要回答您的问题,无法使用JavaScript更新说明元标记,以使更改将反映在“查看源代码”中。但是,您可以更新meta description标签,以便所有浏览器插件(例如书签应用等)都可以看到更新后的描述。为此,您将执行以下操作:

var metaDesc = angular.element($rootElement.find('meta[name=description]')[0]);
metaDesc.attr('content', description);

0
投票

我调整了找到的答案How to dynamically change header based on AngularJS partial view?,以使其在我的网站上起作用。您在路由配置中建立元内容,然后将函数绑定到$routeChangeSuccess事件,以将已配置的值放入$rootScope。只要您的元标记绑定到$rootScope值,一切都会按计划进行。

angularApp.run(['$rootScope', function ($rootScope) {
    $rootScope.$on('$routeChangeSuccess', function (event, current, previous) {
        $rootScope.title = current.$$route.title;
        $rootScope.description = current.$$route.description;
        $rootScope.keywords = current.$$route.keywords;
    });
 }]);

angularApp.config(function ($routeProvider, $locationProvider) {
    $routeProvider
        .when('/About', {
            templateUrl: 'Features/About/About.html',
            title: 'Here\'s the Title',
            description: 'A Nice Description',
            keywords: 'Some, Keywords, Go, Here'
        });

    // use the HTML5 History API
    $locationProvider.html5Mode(true);
    $locationProvider.hashPrefix('!');
});

<head>
    <meta charset="utf-8" />
    <meta name="keywords" content="{{keywords}}"/>
    <meta name="description" content="{{description}}">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="fragment" content="!" />

    <title ng-bind="title">A Placeholder Title</title>
    <link rel="icon" href="/Images/Logo.ico">
    <base href="/" />
    @Styles.Render("~/Content/css")
</head>
© www.soinside.com 2019 - 2024. All rights reserved.