示例:
<key id="1">
<value>login</value>
<description>This is used in login screen</description>
<length>10</length>
<height>20</height>
</key>
答案 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'));