Last updated at Mon, 06 Nov 2017 21:07:00 GMT
The post assumes at least a basic knowledge of Angular. Angular is a very opinionated framework so make sure you have some experience with Angular before following the instructions presented below.
Logentries can integrate into whatever Javascript framework you want to use. Previously, we examined adding Logentries to a React application. This post will illustrate how to add Logentries to your Angular v1 application using a Provider. Angular v1’s Provider architecture provides a robust and modular way to add functionality to your Angular applications.
The Angular Provider architecture is one of Angular v1’s great fixtures. The documentation for Providers is located here. Providers are self-contained objects that are injected into and used inside Angular applications. They are the building blocks of most Angular applications and enforce a modular structure that promotes reuse and clean code. There are a few types of Providers; we are going to use the Factory type.
An Angular Factory is a custom object created using the Angular factory method. Passing a factory into your Angular module through the injection process will make your custom object accessible inside the module. Usually, the Factory is used to create an object that is created only once, and returns a shared object. The Logentries client side service fits nicely with the Factory pattern since it also functions as a shared resource. The bulk of the code used in the factory is to initialize the Logentries object. Once initialized, the Factory exposes the Logentries object.
Let’s start by looking at index.html:
太阳城
Sports-betting-info@rf518.com
智博网
乐贝网电费查询网
澳门金沙
Crown-camp-marketing@zaibj.net
2024欧洲杯竞猜
广州易登网
国电武仪
Sabah-Official-website-media@dienmaythanhlong.net
Sun-City-contactus@pronewport.com
Football-platform-marketing@xsdvoip.com
名品导购网
《龙之力量》官方网站
esball-customerservice@hongjiuchina.com
粤语e族
Sports-betting-platform-marketing@kongtiao11.com
易天富基金网
Crown-Sports-feedback@symmjg.com
文化中国
鄂尔多斯百姓网
哈尔滨新浪乐居
中舟环保
24Mail--查错网
青岛海景花园大酒店官方网站
红粉女性网
易登网
同花顺股民学校
车鉴定
西宁天气预报
站点地图
江阴农商银行
慈溪房产网
FANCL官网
The ng-app tag identifies the main module Angular is loading into the body. The LeAngularSample is the sample module being used to illustrate the library. LogEntriesFactory.js introduces the Logentries library. The code in app.js injects and uses the Logentries code, and inserts the UI component of the sample into the custom “
The index.html is the opening page. Let’s look at the script files being imported, starting with the LogEntriesFactory.js file:
angular.module('LogEntries',[])
.factory('LoggerFactory', function (){
var opts = {};
opts.token = '1234_fake_token';
LE.init(opts);
return LE;
})
The code starts by creating a new Angular module called “LogEntries.” This module is an independent Angular module capable of being used anywhere. This file could be lifted from this application as is and inserted into any application.
The module creates an object called the LoggerFactory. The LoggerFactory is used in the code to interact with the Logentries object. The LoggerFactory starts up, sets the Logentries token, initializes the object per Logentries documentation, and returns the prepared Logentries object.
The other script file houses the directive. The directive looks like this:
angular.module('LeAngularSample', ['LogEntries'])
.directive('selection', function (){
return {
restrict: 'E',
transclude: true,
scope: {},
templateUrl: 'template.html',
controller: function ($scope, LoggerFactory){
$scope.sendError = function (msg){
LoggerFactory.error('Error: ' + msg);
};
$scope.sendWarn = function (msg){
LoggerFactory.warn('Warn: ' + msg);
};
$scope.sendInfo = function (msg){
LoggerFactory.info('Info: ' + msg);
};
$scope.sendLog = function (msg){
LoggerFactory.log('Log: ' + msg);
};
}
}
})
The directive is Angular’s mechanism for creating UI elements. Our directive replaces the
- Injector – angular.module(‘LeAngularSample’, [‘LogEntries’])
LogEntries is the name of the module that contains the LoggerFactory. This line tells Angular to include the LogEntries module into the LeAngularSample module and to make the components of the LogEntries module accessible. - templateUrl – templateUrl: ‘template.html’******
******Angular replaces the selection tag with the contents of the template file at the templateUrl - controller – controller: function ($scope, LoggerFactory){
The controller includes code invoked by the members of the selection directive. Notice the functions in the controller are called in the ng-click of the template. Each link from the template will invoke its counterpart function from the controller. The LoggerFactory can be included since the module was injected above. It is in the controller function parameters so it can be used in the controller’s code.
The template html file looks like this (with some text removed):
….