我怎么可以更新angularjs meta标签? [英] how can i update meta tags in angularjs?

查看:170
本文介绍了我怎么可以更新angularjs meta标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用angularjs开发应用。我想uppdate路线变化的meta标签。

我如何更新angularjs meta标签,可以在页面的查看源文件?

显示

下面是一个HTML code -

 <!DOCTYPE HTML>
    < HTML NG-应用=应用程序>
        < HEAD>
            < META NAME =视口CONTENT =WIDTH =设备宽度,初始规模= 1.0>
            <!META NAME =片段的内容= />
            < META NAME =标题CONTENT =测试应用程序>
            < META NAME =说明内容=测试应用程序>
            < META NAME =关键词内容=测试,应用>            <链接rel =stylesheet属性HREF =CSS / jQuery的-UI-1.10.2.custom.min.css/>
            <链接rel =stylesheet属性HREF =CSS / extra.css/>
            <脚本SRC =JS /库/ jQuery的-1.8.3.min.js>< / SCRIPT>
            <脚本SRC =JS /库/ jQuery的-UI-1.10.2.custom.min.js>< / SCRIPT>
            <脚本SRC =JS /库/ angular.min.js>< / SCRIPT>
            <脚本SRC =JS / controller.js>< / SCRIPT>
            <脚本SRC =JS / routes.js>< / SCRIPT>
        < /头>
        <身体GT;
            < D​​IV NG控制器=mainCtrl级=主容器装载>
                < D​​IV CLASS =集装箱持有人>
                    < D​​IV CLASS =容器>
                        < D​​IV NG-包括SRC ='的元素/ header.html中'>< / DIV>
                        < D​​IV NG-view类=clearfix>< / DIV>
                    < / DIV>
                < / DIV>                < D​​IV NG控制器=userCtrlID =测试>
                    < D​​IV CLASS =容器类=登录容器>
                        < D​​IV ID =登陆车标>
                            < IMG SRC =图像/徽标300.pngALT =级=登录-IMG/>
                            < BR />
                            < D​​IV NG-视图>< / DIV>
                        < / DIV>
                    < / DIV>
                < / DIV>
        < /身体GT;
    < / HTML>


解决方案

 < HTML NG-应用=应用程序>
    <标题NG绑定=metaservice.metaTitle()>测试与LT; /标题>
    < META NAME =说明内容={{metaservice.metaDescription()}}/>
    < META NAME =关键词内容={{metaservice.metaKeywords()}}/>
<脚本>
    VAR应用= angular.module('应用',[]);
    app.service('MetaService',函数(){
       VAR标题='Web应用程序';
       VAR metaDescription ='';
       VAR的MetaKeywords ='';
       返回{
          设置:功能(newTitle的newMetaDescription,newKeywords){
              的MetaKeywords = newKeywords;
              metaDescription = newMetaDescription;
              标题= newTitle的;
          },
          metaTitle:函数(){返回称号; },
          metaDescription:函数(){返回metaDescription; },
          的MetaKeywords:函数(){返回的MetaKeywords; }
       }
    });   app.controller('myCtrl',函数($范围,$ rootScope,MetaService){
      $ rootScope.metaservice = MetaService;
      $ rootScope.metaservice.set(Web应用程序,递减,等等等等);
   });
< / SCRIPT>
 <机身NG控制器=myCtrl>< /身体GT;
< / HTML>

I am developing application using angularjs. I want to uppdate meta tags on route change.
How can I update meta tags in angularjs which can be shown in "view source" of the page ?

here is a html code -

  <!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>

解决方案

<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>

这篇关于我怎么可以更新angularjs meta标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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