Files
angular-docs-cn/aio/src/app/embedded/code/code-example.component.ts
T

48 lines
1.4 KiB
TypeScript
Raw Normal View History

2017-03-26 13:32:29 -07:00
/* tslint:disable component-selector */
import { Component, ElementRef, OnInit } from '@angular/core';
/**
* An embeddable code block that displays nicely formatted code.
* Example usage:
*
* ```
* <code-example language="ts" linenums="2" class="special" title="Do Stuff">
* // a code block
* console.log('do stuff');
* </code-example>
* ```
*/
@Component({
selector: 'code-example',
template: `
<header *ngIf="title">{{title}}</header>
<aio-code [ngClass]="{'headed-code':title, 'simple-code':!title}" [code]="code"
[language]="language" [linenums]="linenums" [path]="path" [region]="region"></aio-code>
2017-03-26 13:32:29 -07:00
`
})
export class CodeExampleComponent implements OnInit {
2017-03-26 13:32:29 -07:00
code: string;
language: string;
linenums: boolean | number;
path: string;
region: string;
2017-03-26 13:32:29 -07:00
title: string;
constructor(private elementRef: ElementRef) {
const element = this.elementRef.nativeElement;
2017-03-26 13:32:29 -07:00
this.language = element.getAttribute('language') || '';
this.linenums = element.getAttribute('linenums');
this.path = element.getAttribute('path') || '';
this.region = element.getAttribute('region') || '';
2017-03-26 13:32:29 -07:00
this.title = element.getAttribute('title') || '';
}
ngOnInit() {
// The `codeExampleContent` property is set by the DocViewer when it builds this component.
// It is the original innerHTML of the host element.
this.code = this.elementRef.nativeElement.codeExampleContent;
}
}