加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 头部的角度动态元标记

发布时间:2020-12-17 17:05:55 所属栏目:安全 来源:网络整理
导读:我需要在角度应用程序中的特定页面上插入开放图形元标记. 这些标签根据页面的新闻或视频而有所不同. 我尝试在$rootscope中添加一个变量.当变量相关时,将使用元标记填充此变量. 现在这是我的问题.只要此变量填充了HTML字符串,它们就不会形成头部的一部分而是
我需要在角度应用程序中的特定页面上插入开放图形元标记.

这些标签根据页面的新闻或视频而有所不同.

我尝试在$rootscope中添加一个变量.当变量相关时,将使用元标记填充此变量.

现在这是我的问题.只要此变量填充了HTML字符串,它们就不会形成头部的一部分而是输出到正文.我搜索了一天,找不到任何可行的解决方案.任何帮助,将不胜感激

解决方法

我创建了一个 Angular module,可以使用$routeProvider路由定义动态设置元标记.

angular.module('YourApp','ngMeta')
.config(function ($routeProvider,ngMetaProvider) {

  $routeProvider
  .when('/home',{
    templateUrl: 'home-template.html',meta: {
      //Sets 'Home Page' as the title when /home is open
      title: 'Home page',description: 'This is the description of the home page!'
    }
  })
  .when('/login',{
    templateUrl: 'login-template.html',meta: {
      //Sets 'Login Page' as the title when /login is open
      title: 'Login page',description: 'Login to this wonderful website!'
    }
  })
});

然后,您可以像这样在HTML中设置元标记

<title ng-bind="ngMeta.title"></title>
<!-- OR <title>{{ngMeta.title}}</title> -->    

<!-- This meta tag can be set using ngMetaProvider -->
<meta property="og:type" content="{{ngMeta.ogType}}" />

<!-- Default locale is en_US -->
<meta property="og:locale" content="{{ngMeta.ogLocale}}" />

<!-- This meta tag changes based on the meta object of each route -->
<!-- or when the setDescription function is called -->
<meta name="description" content="{{ngMeta.description}}" />

要动态设置标题,描述和og:图像,可以将ngMeta注入控制器

.controller('DemoCtrl',function(ngMeta) {

    ngMeta.setTitle('Demo page');
    ngMeta.setDescription('This is the description of the demo page');
    ngMeta.setOgImgUrl('http://example.com/abc.jpg');
});

支持更多标签和ui-router正在开发中.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读