Angular 2 - 国际化 - 包括自定义元数据信息?

时间:2017-02-07 10:38:20

标签: angular internationalization

  • 是否可以在Angular2中获取元数据信息 默认工具(i18n)?

示例:

<key id="1">
<value>login</value>
<description>This is used in login screen</description>
<length>10</length>
<height>20</height>
</key>

  • 是否还有其他支持元数据信息的Angular 2国际化工具?

1 个答案:

答案 0 :(得分:0)

Angular内部i18n工具没有实现操作元标记,但它计划用于Angular Universal according to ocombe

您可以使用Angular 4中包含的Meta service来操作元标记,并编写自己的方法。

Meta添加到app.component.ts

import { Meta } from '@angular/platform-browser';

Meta添加到您的构造函数中:

constructor(private meta: Meta) {

添加元标记

this.meta.addTag({ name: 'test', content: 'test1' });
// or many at once:
this.meta.addTags([
  { name: 'test', content: 'test2' },
  { name: 'test', content: 'test 3' },
  { name: 'testX', content: 'test 4' },
  { name: 'testY', content: 'test 5' },
  ]);    

获取元标记

// only gets the first occurrence if the attribute is used multiple times
console.log('tag: ' + this.meta.getTag('name=test').content);

// gets the outer html
this.meta.getTags('name="test"').forEach(value => console.log('html:', value));
// gets the content
this.meta.getTags('name="test"').forEach(value => console.log('content: ' + value.content));
// gets the object
console.log('object: ', this.meta.getTags('name="test"'));

更新元标记

// only updates the first occurrence!
this.meta.updateTag({name: 'test', content: 'abc'});

删除元标记:

// only deletes the first occurrence
this.meta.removeTag('name="test"');
// does the same as above but takes an `HTMLTagElement`instead of an attribute selector
this.meta.removeTagElement(this.meta.getTag('name=test'));