Skip to content

Instantly share code, notes, and snippets.

View wholypantalones's full-sized avatar

Jason Dare wholypantalones

View GitHub Profile
@wholypantalones
wholypantalones / set-config.ts
Last active October 10, 2018 16:46
Angular TypeScript set config block callback
// Usage: https://stackblitz.com/edit/angular-config-callback
import {Component, OnInit} from '@angular/core';
@Component({
selector: 'app-projects-something',
templateUrl: 'app-projects-something.html',
styleUrls: ['app-projects-something.scss']
})
export class SomethingSomethingComponent implements OnInit {
config: object;
@wholypantalones
wholypantalones / app-routing-module.ts
Created August 10, 2018 17:26
Angular 6 routerLinkActive with params workaround
import {NgModule} from '@angular/core';
import {RouterModule, Routes} from '@angular/router';
import {DashboardViewRouteComponent} from '@routes/dashboard-view-route/dashboard-view-route.component';
import {ProjectsViewRouteComponent} from '@routes/projects-view-route/projects-view-route.component';
import {ProjectsViewDetailsRouteComponent} from '@routes/projects-view-details-route/projects-view-details-route.component';
import {ProjectsAddViewRouteComponent} from '@routes/projects-add-view-route/projects-add-view-route.component';
import {ProjectsViewDetailsTableComponent} from '@routes/projects-view-details-table/projects-view-details-table.component';
const routes: Routes = [
{path: '', redirectTo: 'dashboard', pathMatch: 'full'},
@wholypantalones
wholypantalones / form-data.js
Last active July 25, 2018 14:36
Angular 6.x.x multipart form data post
// Completey omit the 'ContentType: multipart/form-data' headers
// const httpUploadOptions = {
// headers: new HttpHeaders({
// 'Content-Type': 'multipart/form-data'
// })
// };
const str = JSON.stringify('your Blob or Array');
const fd = new FormData();
const file = new File([str], 'fileName');
@wholypantalones
wholypantalones / form-data.js
Last active July 25, 2018 14:28
Angular 1.x.x multipart form data post
// angular.identity: see https://softwareengineering.stackexchange.com/a/283119
// Content-Type: undefined: see https://www.uncorkedstudios.com/blog/multipartformdata-file-upload-with-angularjs/
// new File([data], file): must be a blob or array see https://developer.mozilla.org/en-US/docs/Web/API/Blob
var fd = new FormData(),
file = new File([data], objectName);
fd.append('file', file);
return $http.put('path/to/url', fd, {
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex
arr.findIndex((el) => {
return el.val === another.val;
});
arr.map(function (o) {return attr;}).indexOf(key)
this.http.get('https://jsonplaceholder.typicode.com/users')
.subscribe((users) => {
this.users = users;
}, (err) => {
console.log(err);
});
@wholypantalones
wholypantalones / route-de-route-de-route.js
Created April 20, 2018 15:33
Angularjs route params
var lastRoute = $route.current;
console.log(lastRoute);
$scope.$on('$locationChangeSuccess', function (event) {
if (lastRoute.$$route.originalPath === $route.current.$$route.originalPath) {
$route.current = lastRoute;
}
});
@wholypantalones
wholypantalones / reduce-totals.js
Created April 16, 2018 18:39
reduce totals function
getTotals(items, prop) {
return items.reduce( function(a, b) {
return a + b[prop];
}, 0);
}
@wholypantalones
wholypantalones / searchPipe.ts
Last active September 11, 2018 14:33
Angular 4 search pipe for an array of objects
// *ngFor="let row of (sample | searchPipe: input.value)"
@Pipe({
name: 'searchPipe',
pure: false
})
export class SearchPipe implements PipeTransform {
transform(sample: any[], searchTerm: string): any[] {
if (!searchTerm) return sample;
searchTerm = searchTerm.toLowerCase();
if (sample) {