Add highlight.js to an Angular 2 application
source link: https://marco.dev/2017/02/13/add-highlight-js-to-an-angular-2-application/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
java-version.com: What's new in Java 16? 15? Keep up to date!
Add highlight.js to an Angular 2 application
With the goal to build my own blog application (for educational purposes) I integrated the highlight.js library in the demo application.
At the moment there are problems with safari and mobile platforms, a js error in the library.
The implementation is based on this answer in Stack Overflow (thanks to the author) 😉
In packages.json you have to import the highlight library and the type:
'highlight.js': '^9.9.0', '@types/highlight.js': '^9.1.9',
The implementation is done via a directive:
import {Directive, ElementRef, AfterViewInit} from '@angular/core'; import * as hljs from 'highlight.js'; @Directive({ selector: 'code[ highlight]' // css selector for the attribute }) export class HighlightCodeDirective implements AfterViewInit{ constructor(private eltRef:ElementRef) { } ngAfterViewInit() { hljs.highlightBlock(this.eltRef.nativeElement); } }
The html code of the example:
and the result:
Recommend
-
7
@rodrigokamadaRodrigo KamadaSoftware developer currently working with Node.js, Java, Angular, Bootstrap, MongoDB, MySQL, Redis, Kafka and Docker.Intro...
-
3
...
-
10
Teams Application highlight: Perfect Wiki Microsoft Teams application store has a big number of applications in it. It means also that it can hold in apps that could be useful to many organizations – but they just don’t k...
-
3
Highlight launches full-stack application monitoring platform, raises $8M
-
3
As web apps grow in their capabilities, we also see an increase in their bundle sizes. At one hand we need to ship more code for cool new features. On the other hand, we also need third party libraries and their code for everything to work.
-
2
Angular ...
-
21
In this article, we’ll learn how to add a simple loading spinner in Angular using RxJS and then use an HttpInterceptor to automatically show a loader on all network calls in our Angular app. Why a loading spinner? But fi...
-
1
Angular ...
-
6
ngClass is a directive in Angular that adds and removes CSS classes on an HTML element. In this article, we are talking about ngClass in Angular only, not ng-...
-
3
In this article, we’ll learn how to add async validation to your Angular reactive forms. We’ll build an email input field – which shows up validations when the user focuses away from it. We want the email field to show errors when it...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK