Skip to content

Angular Service that contains methods of the problems that I have encountered during development.

Notifications You must be signed in to change notification settings

baldonharris/Angular-4-Helper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 

Repository files navigation

Angular4 Helper

This is just an Angular Service that contains methods of the problems that I have encountered during development.

Dependencies

  • Angular ^4.0.0
  • Underscore.js

Install dependency

Underscore.js

  1. Install underscorejs in your library
$ npm install underscore --save
$ npm install @types/underscore --save
  1. Edit tsconfig.app.json and add underscore to array types
"types": [
    "underscore"
]

How to use

  1. Import HelperService in your Component
import { HelperService } from 'location/helper.service';
  1. Add HelperService as a provider
@Component({
    ...
    providers: [HelperService]
})
  1. Add HelperService in the constructor as private parameter
constructor(
    private helper: HelperService
) { }

Methods

getRouteData(Router, fn): void

A subscriber of router.events that gets the data of the activated route. Data is passed through the callback, as well as the instanced Subscriber of the event.

Route:

{
    path: '/home',
    component: HomeComponent,
    data: {foo: 'foo', bar: 'bar'}
}

Component:

routerSubscriber: any;

constructor(
    private router: Router,
    private helper: HelperService
) {
    helper.getRouteData(router, (routeData, subscriber) => {
        console.log(routeData.foo);
        console.log(routeData.bar);
		this.routerSubscriber = subscriber;
    });
}

ngOnDestroy() {
    this.routerSubscriber.unsubscribe();
}

About

Angular Service that contains methods of the problems that I have encountered during development.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published