Skip to content

Instantly share code, notes, and snippets.

@simon04
Last active Jun 11, 2021
Embed
What would you like to do?
Font Awesome directive for Angular.js 1.x
import angular from 'angular';
import fontawesome from '@fortawesome/fontawesome-svg-core';
import {faPrint} from '@fortawesome/free-solid-svg-icons/faPrint';
import {faSearch} from '@fortawesome/free-solid-svg-icons/faSearch';
fontawesome.library.add(faPrint, faSearch);
function directive(): ng.IDirective {
return {
restrict: 'E',
link(_scope: ng.IScope, elem: JQLite, attrs: ng.IAttributes) {
const prefix = 'fas' as fontawesome.IconPrefix;
const iconName = attrs.fas as fontawesome.IconName;
const icon: fontawesome.Icon = fontawesome.icon({prefix, iconName});
if (!icon) {
console.warn(`No icon found for `, iconName);
return;
}
elem[0].append(icon.node[0]);
}
};
}
// Usage: <font-awesome fas="print"></font-awesome>
export default angular.module('ngFontAwesome', []).directive({fontAwesome: directive});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment